C# How to access a variable in a script from an instance with the same script?


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;
			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;
			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.

Take a look at some tuts on doing this,

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