- Home /
C# How to access a variable in a script from an instance with the same script?
Hello.
I have set up some code which instantiates and gives tags to a couple of objects. This works fine.
When one of the objects are clicked on, its tag is stored idStart = gameObject.tag;
when another object is clicked I store its tag in the same manner.
So to avoid confusion. Object 1 is pressed, object 2 is pressed. Object 2's script then changes the variable in object 1's script. How would I go about doing this.
This is the code in the script where everything is to be handled. At the bottom, the line dotScript idStart.dotValue--;
is supposed to change the value of object 1's dot value
if (!isClicked)
{
isClicked = true;
//dotValue-=10;
idStart = gameObject.tag;
transform.localScale = new Vector3 (dotValue / 100, dotValue / 100, dotValue / 100) * 5;
xStart = transform.position.x;
yStart = transform.position.y;
zStart = transform.position.z;
}
else
{
//dotValue+=10;
idEnd = gameObject.tag;
c = 10;
transform.localScale = new Vector3 (dotValue / 100, dotValue / 100, dotValue / 100) * 5;
isClicked = false;
xEnd = transform.position.x;
yEnd = transform.position.y;
zEnd = transform.position.z;
for (int i = 0; i<=c;i++)
{
Instantiate(capsualPrefab,new Vector3(xStart+Random.Range(0.1f,1.9f),yStart+Random.Range(0.1f,1.9f),zStart+Random.Range(0.1f,1.9f)),Quaternion.identity);
dotScript idStart.dotValue--;
}
}
Any help is appreciated.
Thanks for your time and I will supply any further information if it is required.
Answer by getyour411 · Jan 31, 2014 at 12:08 AM
Take a look at some tuts on doing this,
http://unitygems.com/script-interaction1/
GameObject newObj = GameObject.FindObjectWithTag("someTag").GetComponent<someClass>();
or similar structure, depending on your needs. That site has great stuff.
Post back if that site doesn't get you moving
Thanks for the help, however, I cannot see why the line of code you are giving me is giving me an error Assets/dotScript.cs(53,88): error CS1061: Type
UnityEngine.GameObject[]' does not contain a definition for GetComponent' and no extension method
GetComponent' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)
That site was very helpful for understanding but did not help with progressing my problem.
Show me the exact lines you are trying? It looks like there's something in an Array which I didn't have referenced in my example.
This is the line I am trying.
GameObject newObj = GameObject.FindGameObjectsWithTag(idStart).GetComponent<dotScript>();
It seems to have a problem with GetComponent right at the end. I do have an array in use either.
EDIT:
This line was wrong because I put an s in by mistake. However, I am apparently trying to convert type dotScript to a GameObject? Is this correct according to the code I have given.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Javascript not being updated, variables being overridden, but C# is fine 1 Answer
Can't access the script of an instantiated prefab :( 2 Answers
Editing a variable from another script on collision 3 Answers
Using a script as a member variable in another script. 2 Answers