Script array problem

Hey everyone,

I have a script array and I would like to call a function from a script in the array.

currentScript = (MonoScript)AllScripts[0];

currentScript.OnMoving();

That last part doesn’t work, ofcourse, but how do I do this correct?

Thanks!

You need to cast it to the type of the script (filename) I believe. If you post the full code we might be able to help more.

This is all related code:

public MonoScript currentScript;

void OnEnable()
{
	Object[] AllTools = Resources.LoadAll("PlayerScripts/", typeof(MonoScript));
	currentScript = (MonoScript)AllScripts[0];
	Debug.Log("Enabled Script = " + currentScript.GetClass());
}

void Update() 
{		
	currentScript.ScriptUpdate();
}

Later on I want “currentScript” to be changed based on buttons the player clicks on.
The point is that I can add scripts to the PlayerScripts folder and it knows what to do
with it automatically.

The only problem is the cast itself. I need to cast the script file to the name of the script file, but that changes every time.

How can I accomplish this?

What you are trying to accomplish here is fairly complicated. Perhaps a different approach here may work better for you.

Why don’t you try to AddComponent all your target scripts on an empty game object? Then, instead of parsing a generic array to get a type specific element, you can GetComponent the script you want. GetComponent allows for a string overload and if your naming convention has an iterator, ie “Script001”, “Script002” etc, you can construct the script name as a string and pass it as an argument to GetComponent.

Why not just use an ArrayList? That way you can store the scripts without having to cast them when gaining access?

Thanks for responding so quick Diviner. I like your solution to my problem, but I still would really like to know how it’s done in the “fairly complicated” way. Just because I want to learn how that kind of stuff works and improve my c# knowledge :). If you could give me a push in the right direction, maybe a link or a small example?

No, you still have to cast with ArrayList. With List, you don’t.

–Eric

?

Unless I am not understanding what he is doing right or Unity is backwards ArrayLists do not care what type you put in them, Lists do.

List name = new List();
ArrayList name = new ArrayList();

Thanks all for responding. I tried to get it to work today based on several comments. Everything give me problems, probably because I have not explained the thing I’m trying to accomplish well enough.

After reading on other fora I found out that I’m trying to “invoke a method”, from another script usinging a variable
for the script itself.
Right now I’m trying this:

		Type thisType = this.GetType();
		MethodInfo theMethod = thisType.GetMethod("scriptUpdate");
		theMethod.Invoke(this, null);

I have “using System;” and “using System.Reflection;” at the top.

For some reason I get the error:
Object reference not set to an instance of an object.

I’m really new to all this scripting stuff. It would be nice if someone could explain why I get this error, and
how to solve the problem :).

hhmmm… now that I read something more on this, it might not be doing what I thought it was doing…