How to load a script directly from resources folder?

I’m trying this way:

var Database;

function Start(){
Database = Resources.Load("Database");}

but when i try to get a variable from there like:

print(Database.foo);

It prints null.

If i create a prefab named Database and attach the database script to it and load like this:

Database = Resources.Load("Database",GameObject).GetComponent("Database");

…It works correctly, but is there another a way to load an script without have to create a prefab?

Scripts have to be on GameObjects. They can’t just be floating around by themselves.

So, i tried this way too:

gameObject.AddComponent(Resources.Load("Database"));

But it give this error:
MissingMethodException: Method not found: ‘UnityEngine.GameObject.AddComponent’.

You should write a type instead of a resource load. gameObject.AddComponent (typeof (Database));

1 Like

You can have Singleton scripts that don’t inherit from MonoBehaviour.

Anyway, I don’t think you can Resource.Load() them. They should be accessible anyway.

MyScript ms = (MyScript) go.AddComponent(typeof(MyScript));

Thanks for the advices folks, i got it.
Now, how i make to keep the script values of my variables in the inspector? There’s a way?

A newly attached component is only going to have the defaults value from what I know.

If you doing it by script, why not assign them by script?

MyScript ms = (MyScript) go.AddComponent(typeof(MyScript));
ms.someVariable = 123;

1 Like

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… =/

@System.NonSerialized ! Gotchaa!

I need to access a script in a gameObject that does not inherit MonoBehaviour…the above mentioned method didn’t helped me…got any other suggestions??

I didn’t think you can have a script attached to a GameObject that doesn’t inherit from MonoBehaviour, in fact it gives an error if you try.

1 Like

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.

Resources.Load is only required for assets.

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.