Execute an script's Start() after another script's Start() Unity C# - unity3d

I would like to run the Start() from one script just before the Start() of another Script. Is it possible? Can you choose the order of the executions of the scripts?

I am not totally sure about Start() but you can configure the Script Execution Order of Awake, OnEnable and Update. Go to menu Edit / Project Settings and set your preferences like described in the manual section. So you might want to investigate further if Start is affected too - I believe it is as it is kind of related to Update
In general I would recommend to use this feature carefully. If you run nto the situation of having too many scripts in this list, this indicates some design issues.

If you have one script (A) meant to run after another (B), I guess it means A depends on B. In that case, you should get B to call for A passing the needed data.
public class A : MonoBehaviour
{
public void Init(State state){}
}
public class B : MonoBehaviour
{
private State state;
void Start()
{
this.state = SetState();
this.gameObject.GetComponent<A>().Init(this.state);
}
}
This might be the only way in the long run preventing long debugging hours. In fact, if you use the script execution order, it is fine until you have a lot of classes and you have been working on the project for 6 months or more. Worst, you give the project to another coder. Then you have "invisible" dependencies with new bugs you can hardly debug since they are not in the code.

What you may be able to do is normally do the script that you are wanting first however you would like it. However lets say your wanting to run the other script at the end of the first script, You may be able to reference a function by using this (Replacing SecondScriptName with the script you want to go after the first one then replacing FunctionFromSecondScript with the Function from that script)
<SecondScriptName>().FunctionFromSecondScript();
You can then call all of the functions in turn in whatever order you wish.
If i make a mistake, Please forgive me as this is my first comment to help another programmer and I am currently a budding one myself.
Hope this helps :)

Related

Mono.Cecil and Unity not playing nice together

I'm trying to use Mono.Cecil to patch my custom user scripts in Unity.
I have code I want to inject into my custom user scripts in Unity to avoid writing the same lines of code in every MonoBehaviour in the project.
However, when I do:
using (AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(assembly.Location, new ReaderParameters() { ReadWrite = true }))
{
//Do some patching here
assemblyDefinition.Write();
}
Then I get an exception saying
IOException: Win32 IO returned 1224
Which apparently means that the file is locked from being written to.
If I instead try to use:
File.Delete(sourceAssemblyPath);
File.Move(targetAssemblyPath, sourceAssemblyPath);
Then the dll gets patched correctly, but when I try to play the application then the scripts in my scene lose reference, as if the replacement of the file causes them to think the scripts on the scene objects no longer exist in the project (Which I guess would make sense since I DID delete the dll they were in to replace it with the new one).
Has anyone any idea on how to patch the user's project assembly in Unity while maintaining usability of the current project?
Or should I resort to only patching during build or something?
Suggestions?
Thanks
Last time I tried something with Cecil I was able to use a single var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); to both read and write the file without a delete/copy.
If you do it using [InitializeOnLoad] then the assemblies have obviously already been loaded so modifying them at that point isn't going to help; you'd need to load assemblies once to trigger Cecil than reload again to load the modifications every time you would normally reload only once. You'll want to use UnityEditor.AssemblyReloadEvents.beforeAssemblyReload instead.
beforeAssemblyReload gets called after the new assemblies are recompiled but before they are loaded. So you'd use [InitializeOnLoad] to register a callback ([DidReloadScripts] seems identical in every case I've tried) which should ensure that all newly compiled assemblies get processed going forward. In some cases this might not happen (such as if scripts need to be compiled when you first open the editor so it hasn't registered your event yet) so you'll probably also need to run your processing code immediately on initialisation as well and force an assembly reload if anything it changed using UnityEditorInternal.InternalEditorUtility.RequestScriptReload(); or UnityEditor.AssetDatabase.Refresh();.
The best way I've found to mark an assembly as processed is to inject an attribute definition and add an instance of it to the assembly, then check for it by name and skip the assembly if it exists. Without a way to do this, you'd be processing every assembly in the project every time scripts recompile rather than only processing ones that have been modified.
Edit: to process the assemblies of a build, try this:
private static bool _HasProcessed;
[PostProcessScene]
private static void OnPostProcessScene()
{
if (_HasProcessed || !BuildPipeline.isBuildingPlayer)
return;
ProcessAssemblies(#"Library\PlayerDataCache");
_HasProcessed = true;
}
[PostProcessBuild]
private static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
_HasProcessed = false;
}

Starting AnyLogic experiment programmatically

I need to run a large number of experiments and would like to do so over night as to waste as little time as possible. I have some output that I can export using PrintWriter, but I need to be able to start the next experiment programmatically after the other.
So something like
After experiment:
Experiment63.start().run();
If a parameter variation experiment doesn't do what you need and you really need to run mulitple sensitivity analyses, try this:
Create a new Custom Experiment
Delete everything in the properties window
Use YourExperimentClass.main(new String[] {}) to start each experiment.
For example, lets say you have three sensitivity analyses to run:
SensitivityToHeatExperiment.main(new String[] {});
SensitivityToSpeedExperiment.main(new String[] {});
SensitivityToFrictionExperiment.main(new String[] {});
These calls bring up a window for each experiment. Since experiments don't start automatically, you'll need to add that logic if you don't want to click "run" a bunch of times! In each Experiment's Initial experiment setup section, put run();. This automatically starts the simulation for you.
I haven't quite figured out how to close the windows automatically using this approach: system.exit(0) and experiment.close() shut all windows opened by the experiment, so you'll need a way to tell if all experiments are done running. One option is to use a common file and a FileLock to ensure the simulations don't encounter concurrency problems. Note that FileLock might be handy if all sensitivity experiments need to write to common files.

Unity script execution order and Start()

Unity's documentation has this line:
By default, the Awake, OnEnable and Update functions of different scripts are called in the order the scripts are loaded (which is arbitrary).
So I have two questions here:
What does "Arbitrary" mean in this context? Is it random?
Does this also include Start() alongside Awake(), or does Start() have its own special behaviour that doesn't follow the script execution order, even if that order has been altered in the project settings?
I've wondered for a while how Unity "decides" how it behaves when it runs, especially since it seems like something will work some of the time but the rest of the time it causes a crash or something with little to no explanation, but the documentation doesn't really mention it much and I can't seem to find much info elsewhere.
That statement is somehow confusing.
Awake, OnEnable and Update will always be called in order.
1.What does "Arbitrary" mean in this context? Is it random?
Yes, its random. Although, it is not talking about the Awake, OnEnable and Update functions. It is talking about scripts. The scripts are randomly chosen to execute.
2.Does this also include Start() alongside Awake(), or does Start() have its own special behaviour that doesn't follow the script
execution order, even if that order has been altered in the project
settings?
Answer #1 should also answer question #2.
This does not affect the callback functions such as Start() Awake(), or OnEnable().
I've wondered for a while how Unity "decides" how it behaves when it
runs, especially since it seems like something will work some of the
time but the rest of the time it causes a crash or something with
little to no explanation
Yes, this is true. This has happened to me in the past too. This is more prone to happen when you have large project with many scripts. The scripts are called randomly. Sometimes, you can get null exception error because GetComponent failed to work. This is why the Script Execution Order Settings is made so that you can always set the order in which your scripts execute.
What I do to fix problems like this is to perform GetComponent in a coroutine function. After that, I check if it is null. If it is null, wait for one frame then try GetComponent again.
Again, this applies to the order in which your scripts are executed not order in which callback functions are invoked/called.
Question 1
According to https://docs.unity3d.com/Manual/class-ScriptExecution.html you can set the order the scripts load manually. If you don't set an order my guess is that Unity uses some predefined order (random, alphabetical or something)
Question 2
Awake() allways runs before Start() and is called when the script is loaded https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html.
To load the scripts in a specific order you need to use the script execution order. To make sure you run the Start() function of each object in a specific order you need to make a "Manager" object that instantiates the objects in the order you wish.

How to make a 3rd person Controller in Unity 3D continuously run

I am making a racing game in unity like Temple Run where the player needs to constantly run and collect fruits/ power ups. I am using the 3rd person default construction worker to make this work. But can't figure out what to tweak in the script to make it run continuously.. Help me where exactly I need to change this.
I believe you should look in this script: Standard Assets\Character Controllers\Sources\Scripts\CharacterMotor.js.
The easy part would be to set var sprint : boolean = false; to true instead.
If you don't need the walking code, you have to clean up some more code in that script to avoid using unnessecary code lines.
Ok first of all you need animations. Then change them to loop and call them run or turn. then for part of the script.
function Update ()
{
animation.Play("run");
}
Just have a look at similar question.

Easy clock simulation for testing a project

Consider testing the project you've just implemented. If it's using the system's clock in anyway, testing it would be an issue. The first solution that comes to mind is simulation; manually manipulate system's clock to fool all the components of your software to believe the time is ticking the way you want it to. How do you implement such a solution?
My solution is:
Using a virtual environment (e.g. VMWare Player) and installing a Linux (I leave the distribution to you) and manipulating virtual system's clock to create the illusion of time passing. The only problem is, clock is ticking as your code is running. Me, myself, am looking for a solution that time will actually stop and it won't change unless I tell it to.
Constraints:
You can't confine the list of components used in project, as they might be anything. For instance I used MySQL date/time functions and I want to fool them without amending MySQL's code in anyway (it's too costy since you might end up compiling every single component of your project).
Write a small program that changes the system clock when you want it, and how much you want it. For example, each second, change the clock an extra 59 seconds.
The small program should
Either keep track of what it did, so it can undo it
Use the Network Time Protocol to get the clock back to its old value (reference before, remember difference, ask afterwards, apply difference).
From your additional explanation in the comments (maybe you cold add them to your question?), my thoughts are:
You may already have solved 1 & 2, but they relate to the problem, if not the question.
1) This is a web application, so you only need to concern yourself with your server's clock. Don't trust any clock that is controlled by the client.
2) You only seem to need elapsed time as opposed to absolute time. Therefore why not keep track of the time at which the server request starts and ends, then add the elapsed server time back on to the remaining 'time-bank' (or whatever the constraint is)?
3) As far as testing goes, you don't need to concern yourself with any actual 'clock' at all. As Gilbert Le Blanc suggests, write a wrapper around your system calls that you can then use to return dummy test data. So if you had a method getTime() which returned the current system time, you could wrap it in another method or overload it with a parameter that returns an arbitrary offset.
Encapsulate your system calls in their own methods, and you can replace the system calls with simulation calls for testing.
Edited to show an example.
I write Java games. Here's a simple Java Font class that puts the font for the game in one place, in case I decide to change the font later.
package xxx.xxx.minesweeper.view;
import java.awt.Font;
public class MinesweeperFont {
protected static final String FONT_NAME = "Comic Sans MS";
public static Font getBoldFont(int pointSize) {
return new Font(FONT_NAME, Font.BOLD, pointSize);
}
}
Again, using Java, here's a simple method of encapsulating a System call.
public static void printConsole(String text) {
System.out.println(text);
}
Replace every instance of System.out.println in your code with printConsole, and your system call exists in only one place.
By overriding or modifying the encapsulated methods, you can test them.
Another solution would be to debug and manipulate values returned by time functions to set them to anything you want