using UnityEngine;
using System.Collections;
public class Test: MonoBehaviour{
//I want to add a variable here using another script
void Start(){
}
void Update(){
}
}
I know how to update the variables that already exist but how do I create a new variable from another script?
I understand the question… i guess.
If you want to access from script A, a variable declared in script B, you could do…
//In script A
ClassType foo = (ClassType) gameObject.getComponent(typeof("ClassName"));
//show value of variable declared in script B
Debug.Log("value in B script: " + foo.variableThatIsDefinedInScriptB);
As waltermana said, you could -if you need- store foo.variableThatIsDefinedInScriptB into an array, hashtable or whatever you want.
that actually works how i need it … i mean the Array and then accessing it with getcomponent.
Now it seems my problem got more complex. I am clicking objects in a sequence like this:
//sequence
string[] seqString = new string[] {
"Cube001",
"Cube002",
"Cube003",
"Cube001", //It returns to index 0 for this ???
"Cube004",
"Cube002",
"Cube007",
"Cube001",
"Cube003",
"Cube002",
"Cube008",
"Cube005",
"Cube009",
"Cube010",
"Cube001",
"Cube005"
};
The array is in a very specific order. I have a function that should loop through the array using the following code. when I click on a cube it sends this script the object’s name as a “sender” variable.
public void ProcedureHit(string sender){
foreach (string s in seqString)
{
switch (sender.ToLower() == s.ToLower())
{
case true:
switch(seqCurrentStep == Array.IndexOf(seqString, s)) {
case true:
GameObject.Find(sender).GetComponent<Valve>().SetSelected();
seqCurrentStep++;
break;
case false:
//I want to pop a window up here saying wrong valve.
break;
}
break;
}
}
}
The problem is that it works until I get to a duplicate name in the array. Then it chooses the first name it finds in the array instead of the next value in the array’s sequence. Is there a better way to loop through the array? The only other thing I could think is to remove the value from this array and insert it into a new array so that I have a “unfinished” array and a “finished” array.
Hello again.
First of all, i suggest you to change switch statement for if statement, which is more efficient for this kind of evaluation (true/false).
Answering you question, maybe you consider use an auxiliary variable to store the number of attempts of the player, and use it to evaluate the position in the array, and avoid using foreach sentence
You could pass to the function the number of attempt, and the cube pressed… and do the magic more easy… like that.