But if the script is already attached to gameobject and i change, the gameobject keep the old value… There’s a way to lock? I know that it was made justo make things easier but in this case i need to keep the values from the script… =/
Just to answer the original thing: you don’t need resources.load for code more accurately it does not work.
Code is always in the build and its always present, you can just create a new instance of a class, be it with addcomponent if it extends from component or its extended classes, or through new XXXX when it inherits from object and alike.
Then how can i access that script. I need to access a member of the class that does not inherit from monobehaviour.
I gives me a null reference exception if i try to use a instance of the class i am trying to access.
Old topic but this is again a very very powerful way of adding scripts dynamically at runtime in code:
string myScripts_Name = "JumpUpAndDown"; // for example
this.gameObject.AddComponent (Type.GetType (myScripts_Name));
Then inside the JumpUpAndDown you have it destroy itself when it’s done or whatever.
I’m sure you learned this 7 years ago, but to make values ‘global’ in a way what you want to probably do is use DontDestroyOnLoad on a gameobject and attach a ‘global’ script to that game object. People do this for scene manager objects that need to stay present at all times on a scene switch. I also use it for my audio managers as sometimes from one scene to the next you don’t want music to change or stop.
To access a script that doesn’t inherit from monobehaviour, you can (probably even need) create an instance of that script like so:
public class TextBox {
int state { set; get; }
Color color { set; get; } //for example
}
public class GenerateTextBox : MonoBehaviour {
void Start() {
TextBox myTextBox = new TextBox();
myTextBox.state = 0;
myTextBox.Color = new Vector3(255,255,255);
}
}
Personally I use this for like a text box engine.
You cannot use the new keyword on a monobehaviour class, but you can use the new keyword on a class that doesn’t inherit from monobehaviour from within a monobehaviour class.