- Home /
Changing the type of a variable
Here's my question:
I have a variable stands for 1 script, but I want to be able to change the type of the variable so that it won't be a variable for script1 but for script2 if something happened.
(answer with c# please)
You need to explain what you want a bit better. But from what I can tell, you want something that is in script1 to be accessible in script2 if the player does something (scores a point for instance)?
If that is the case. You can do it a couple of ways. You could make the variable in script1 static and access it that from script2 that way or you could have a new variable in script2 which gets the component (variable) from script1.
Nope, I want 1 variable to be able to access script1, and if something happens, the variable must be able to access script2
Haha. This is an entertaining interaction. Variables don't "access" scripts. $$anonymous$$y only guess at what you're talking about would have been the same as gotnoodles.
Answer by Blaveloper · Apr 01, 2014 at 08:51 PM
First off, a variable doesn't access a script, a script accesses a variable.
Secondly, you cannot change the type of a variable without messing it all up (it's not like you can turn a string into a double and assume everything will work, for example).
With the pre-pre-pre-basics of programming behind us, we will now go on to very serious programming:
To access a variable in another script, make them public and static. Like this:
public static int someVariable = 9000;
This variable exists in script1.cs. Now the really hard stuff comes! In script2.cs, type this:
script1.someVariable;
You can access a script by variable like:
private Script1 script;
what I want then is to be able to access Script2 with it if something happens as well, like:
private bool exampleBool;
void Start(){
script = GameObject.Find("player").GetComponent<Script1>();
if(exampleBool == true){
script = GameObject.Find("player").GetComponent<Script2>(); //doesnt work
}
}
$$anonymous$$ay I ask you WHY you do it like that? Your script makes no sense.
Just do however I did it and you should be safe. Unless you're not looking for a variable to be accessed in another script.
@F.N Right well you can do that if both scripts inherit from something that is a common base or both implement the same interface. Perhaps you could explain what you want to do with these scripts? Call similar functions on them? Access similar variables?
@whydoidoit Yea, I have 2 monsters with monster1: script1 attached and monster2: script2 attached. The variables that I have to access in the 2 scripts are mostly the same.
So I think you need to make a base class with the shared variables in and then declare your variable with that base type...
public Shared : $$anonymous$$onoBehaviour {
public string commonVariable;
public int anotherVariable;
}
public Script1 : Shared {
}
public Script2 : Shared {
}
public Shared script;
Answer by FinKone · Apr 01, 2014 at 08:28 PM
Make the same type of variable in script 2, access script 1 in script 2, replace script 2 with 1s value / data and its stored. You can also send it to script 2 from 1 if you want it to happen at a certain point. Depends how when and why you want to back it up or duplicate it. Look into GetComponent to accomplish this type of thing, or acess th script directly in code....
Your answer
Follow this Question
Related Questions
Change gameobject tag when player collides with another gameobject 1 Answer
Is changing the color of GUIText unavailable in the Free Edition? 1 Answer
Change Text of GUI Button from Script 2 Answers
script help gameobject to playercar 1 Answer
How do I keep changes I make to parameters while game is running live? 2 Answers