- Home /
Call the same function on different scripts.
I am trying to call the same function on different scripts. I place all scripts into an array and then try to call that function by iterating trough all scripts.
private var SavableScripts : List.<MonoBehaviour> = new List.<MonoBehaviour>();
public function SaveNewGame() : void
{
5. for ( var i : MonoBehaviour in SavableScripts );
{
i.OnSaveGame();
}
10. // 2do hide saving icon
}
This does not seem to work, it can't find that member function OnSaveGame().
How can I accomplish this, do I have to look into generic functions using c# instead of this unityscript attempt of mine?
I do not wish to call BroadcastMessage and the like because I will not know when saving is completed that way.
C# Would be a lot easier to use. You would create a base class that implemented the OnSaveGame method as virtual. Each derived class would also use the virtual keyword prefix to OnSaveGame. You could then create your list from your new base class.
In javascript, this is the closest workaround I could find:
[link removed as it was JavaScript, not Unityscript/javascript]
@richyrich: What you've linked is EC$$anonymous$$A Javascript and not UnityScript. Unity doesn't support Javascript at all. UnityScript is a completely different language which they unfortunately called Javascript. UnityScript is syntactically similar to Javascript but that's all.
@Bunny83: Thanks. It's all very confusing that something called JavaScript in the Unity program itself, is syntactically similar to JavaScript but not actually supported javaScript. Think I'll stop trying to help with the javascript and stick to c#, which is presumably still c#, not unity c#!
@richyrich: Right ;) UnityScript / Javascript is supposed to make it easier for beginners, however the way it works is completely different from the normal "Javascript". So if a webdeveloper starts using Untiy he will have a lot of "wtf" moments as almost nothing he knows applies to that "Javascript" in Unity.
C# is basically C#. Though since it's $$anonymous$$ono and not .NET the version of C# can't be deter$$anonymous$$ed in relation to the $$anonymous$$SDN docs. Some things might not be implemented (yet). However everything that is supported works exactly the same as in .NET.
Answer by Bunny83 · Nov 21, 2014 at 04:27 PM
You basically have 4 options:
Use Invoke on that MonoBehaviour reference.
Use a base class with a virtual method that gets overridden in your derived classes
Use an interface (that has to be defined in C# as that's not possible in UnityScript afaik) and let your classes implement that interface.
Use reflection to sneak into the acual class, find the method you want to call and invoke it manually.
Invoke does not wait for the invoked function to complete so I'll have a shot at your 2nd suggestion.
I think its this?
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/polymorphism Also UnityScript seems to have limited interface support.
In both cases I will have to make my scripts classes so I can extend them. So I better get started :)