- Home /
Script as a variable being assigned later in script???
I am writing a script, currently it is getting a little big in my opinion (around 500 lines of code)
It was hard to make, so I won't post it here. My question is... I am trying to use GetComponent to get a variable from within another script. The problem is, I want to be able to change which script to get it from using the script as a variable... Example
`var script : ???;
gameObject.GetComponent(script).variable
if(2 == 2){
script = otherscript
}`
See my dilemma? I don't know what type to use for my variable. I thought MonoBehavior, but it throws tons of errors! It would say "variable is not a member of UnityEngine.Component" So can someone help me out, please?
Do you want to be able to change your code quickly to run tests ? Because I would suggest ctrl+h.
Actually, my script is going on an NPC that will detect another NPCs particular AI script, and adjust it's actions accordingly.
Answer by MasterGalaxy · Jun 08, 2012 at 12:26 AM
I had #Pragma Strict on. Without it, everything works fine. I feel like an idiot now. THANKS EVERYONE FOR ALL OF YOUR HELP!!!
Answer by bompi88 · Jun 05, 2012 at 11:43 PM
Does the script variable containing the script have to be "global"? Or could you do something like this?
function yourFunction (whichScriptToUse : int) : float {
var theScript;
switch(whichScriptToUse) {
case 0:
theScript = gameObject.GetComponent(script);
break;
case 1:
theScript = gameObject.GetComponent(otherscript);
break;
}
return theScript.variable;
}
function Start () {
// using script2s variable
var variableFromScript : float;
variableFromScript = yourFunction(0);
Debug.Log("Variable in Script1: " + variableFromScript);
variableFromScript = yourFunction(1);
Debug.Log("Variable in Script2: " + variableFromScript);
}
I like this idea, it could work. I haven't tried it yet, but when I get a chance, I surely will! thank you for this.
I tried implementing this in my javascript/unityscript, but it didn't work. Unfortunately, I'm not sure if my problem is fixable in javascript. If it is, please, I would love to know how.
How did it not work? Did you get errors or did it not do what you want it to do?
I got plenty of errors. Apparently, it's reading the variable script as a string, and for some reason, getcomponent is rejecting it.
BCE0004: Ambiguous reference 'GetComponent': UnityEngine.GameObject.GetComponent(String), UnityEngine.GameObject.GetComponent(System.Type).
What are the name of your script? script.js and otherscript.js? Then try my edited code above.
Answer by Berenger · Jun 05, 2012 at 11:55 PM
I suggest you create an abstract class inherithing from MonoBehaviour class and two (or more) classes inheriting from it. In the script in your question, deal with the abstract class and implement the particular behaviour inside the children class. If it's not clear, tell me and I will add an example.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072415im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
cant add a script because of monobehaviour 1 Answer
Is there a "Lightweight Monobehaviour"? 1 Answer
Scripting Errors I cannot fix, Could use a helping hand and someone to tell me how to fix it. 2 Answers
Getting into Monobehavior 1 Answer
How can I reference a specific script inside a GameObject from another MonoBehaviour. 2 Answers