I’m trying to store a Script in a variable, and can’t seem to get it to work. I’ve searched around, but searching words like “variable” and “script” in a programming forum returns… well… pretty much every thread ever posted.
I have a TriggerInteraction script. This is meant to be open-ended, and attached to anything that the player can interact with. When the player collides with its trigger, it should send the player (actually, the mainCamera) 2 variables. One is the ButtonStyle to be displayed on the players screen, and the other is the Script that will run when the player presses that button.
//TriggerInteraction.js
var GUIScript;//the UI_Game script file
var myIcon:String;//name of the button style
var myScript:String;//name of the script for this interaction
function OnTriggerEnter(other:Collider){
GUIScript=camera.mainCamera.GetComponent(UI_Game);
GUIScript.iIcon=myIcon;
GUIScript.iScript=myScript;
}
function OnTriggerExit(other:Collider){
GUIScript=camera.mainCamera.GetComponent(UI_Game);
GUIScript.iIcon="";
GUIScript.iScript="";
}
Then, in the mainCamera, there is a script, called UI_Game.js
//UI_Game.js
var iIcon:String="";
var iScript:String="";
function OnGUI(){
//if they are within range of an interactive object
if(iIcon!=""){
DrawInteractive();
}
}
//drawn only if the character is within range of an interactive object
function DrawInteractive(){
if(GUI.Button(Rect((Screen.width/2-50),(Screen.height-155),64,64),"",GUI.skin.FindStyle(iIcon))){
interaction=GetComponent(iScript);
interaction.Start();
}
Now, I know that the interaction=GetComponent(iScript) is a bit ridiculous. This is like the 10th different way I’ve tried to get this to work. My previous attempts looked like they made more sense… though they still failed.
Any help is much appreciated. Thanks!
I would also like to add that, yes, I know I can reference the object that contains the script by simply looking for its name, BUT, it is very possible that there will be multiple instances of the object in a single scene.
Help?
trying to think about this makes my head hurt, you have a string that you want to actually run in the program?
Anyway i’d use gameobject.GetInstanceID “The instance id of an object is always guaranteed to be unique.”
edit: sorry, forget that, i never had to use it and i thought there would be an easy way to get to the gameobject
I tried that. In the InteractiveObject, I put
myID=gameObject.GetInstanceID
Then I passed that ID to the UI_Game. But when I tried the GetComponent on that passed variable, I got an error saying that GetComponent couldn’t be used on an Integer (Instance IDs are just integers, and apparently don’t mean anything.) Is there some code for FindInstanceByID? That would be helpful…
Thanks Frederiksen
P.S. It doesn’t have to be a String. I’d be happy with any way I can accomplish this.
okay how about this;
ontriggerenter{active=true}
function update()
{
if(active)
{
if(mainCamera.GetComponent("UI_Game.js").buttonpressed == true)
{
dostuff();
}
}
}
wow that is some ugly code. Sorry, it’s late.
I’m afraid that won’t work. Getting the interactive object to trigger isn’t the problem. Neither is communicating with UI_Game script. The problem comes from trying to store a different script in a variable in the interactive object, then getting the UI_Game to cause it to run.
No worries. Keep the ideas coming though 
I figured this out. Basically, the direct approach I was trying is simply impossible. I was setting the function as a variable (either MonoScript or a String), but I couldn’t call that function because it simply wasn’t a component of the object. So now, I have my InteractiveObject script, which contains the String name of a function, AND I have that function added to the object. I then pass the String name to the UI_Game, it then does a GetComponent, looking for the name that it received. It works just fine.
I suppose I could have an AddComponent line to the interactive object… save me one step… we’ll see. Thanks for the help!
You’ve solved it, but from what I understand it looks like a bit of a roundabout way to do it. Here’s what I’d do.
Say I have a script called Utility.js. I want to call a function (MyUtilityFunction) from that script file within my other file, Main.js.
My Main.js file would look something like this:
var utilityScript : Utility;
function Update()
{
utilityScript.MyUtilityFunction();
}
In the Unity inspector I would attach both files to a GameObject, then drag an instance of Utility to the utilityScript variable in Main.js.
Yes, but the problem is that its not the same function being called every time. I wanted one script that I could attach to every interactive object, then apply a variable to it, which denoted the function that that particular object would call. I then wanted the main script to be able to make a single call on any interactive object and run the appropriate script.
Okay, I see. You want to be able to call the same function from your main script, but have it do something different depending on which object it’s attached to. What you’re describing is an object-oriented concept that’s used fairly often in modern programming, through inheritance or interfaces. I’m not so comfortable with OO concepts in Javascript, but here goes.
What you need is a base class that contains a function called “MyFunction()” or whatever. Then, write a subclass of that class and override the MyFunction() for each different functionality that you need. Then you do what I said above, but instead of Utility, your variable is an instance of the base class.
Then you drag and drop an instance of the subclass to assign them to that variable, and you can call MyFunction on them, and have it do something different based on which subclass you assigned to it. It effectively hides the details from the main script while performing different tasks based on which subclass you use.
Very much so Ahonek. Actually I have a very limited knowledge of OO in javascript, so lets learn a little…
Inheritance is the key here. So lets start off with a test base class, give it a start function and a basic variable to test. We will then extend it onto a new class.
Specifically, now we are going to create a variable based on the base class, but using the extended class as a creator for it.
Now lets test it…
class testBase{
var a=1;
function Start(){
Debug.Log("This is the base class");
}
}
class extendBase extends testBase{
var b=3.0;
function Start(){
Debug.Log("This is the extended class");
Debug.Log(a);
Debug.Log(b);
}
}
function Start(){
var test : testBase = extendBase();
test.Start();
print(test.a);
//print(test.b); // errors
}
I find that the Start function prints what is in the extended class. Also it prints the variable a from the base class and the variable b from the extended class.
I also find that test.a can be obtained from the base class, but since we didnt define the extended class as the core, we cannot reach the variable b from the extended class.
So, with this knowledge, we could call a Start() from any extended class, achieve the results from that extended class but not be hassled with other items in that class that we don’t need access to.
Voila! Thanks BMB, I think we all learned something valuable here.
What do you think, Khan? Are these the droids you’re looking for?
Couldn’t he do something like this?
UtilityScript.js
public var myType : utilityType;
enum utilityType{ type1, type2, type3 };
function MyUtilityFunction(){
switch(myType){
case utilityType.type1:
Type1Function();
break;
case utilityType.type2:
Type2Function();
break;
case utilityType.type3:
Type3Function();
break;
}
}
function Type1Function(){
//do stuff
}
function Type2Function(){
//do stuff
}
function Type3Function(){
//do stuff
}
Of course he could, though be careful with that code sample, you’re comparing an enum type to strings.
However, if he did that he would have to add new functions and new switch elements whenever he wanted to create a new functionality. It’s not a super elegant solution.
With an object-oriented approach, he would just extend his base class and override his function, and treat an instance of the extended class as a base class. It’s the standard OO approach, and it is the way the ToString() method is implemented, for example. In fact, ToString() is probably the ideal example of this type of architecture.
Oops, my bad. I wrote that off the cuff, I meant to compare states, fixed it.
I am lazy and have very little OOP experience, so I tend to do everything procedurally like that 