Script changing variable on prefab in Project, not Heirarchy

Hi there,

This is a new weirdness for me. I have a Prefab with a public variable called quantity. That prefab was dragged into the scene. The quantity variable should be decremented when a button is pressed.

It is the original prefab, in the Project window, that is changed instead. When I create two objects from the prefab, they are sharing the same variable. So I have two questions.

  1. I thought dragging a Prefab into the Heirarchy created different instances of the object. Why are they sharing variables, how do I make them unique?

  2. This value also stays changed on the original Prefab when exiting Play Mode. Up to this point, nothing has persisted when exiting Play Mode. What’s going on there? (I haven’t set it to save anything between plays.)

Thanks,

Victor

use this to find the script with the quantity variable:

var thescript = gameObject.GetComponent(“SCRIPT NAME”);

use this to access the variable:

thescript.quantity

(this works in unityscript, not sure about C#)

Thanks for answering!

Variable is public, not static.

I have set the value using this.quantity, and quantity. This.transform.quantity returns an error.

Even if I am finding the Prefab on accident, why is the change staying after I leave Play mode?

void WhereToUse(Tap tap){

	if (dfInputManager.ControlUnderMouse == null)
	{

		Debug.Log("Plant is " + plant.name);

		Debug.Log("Plant here");

		Debug.Log("Tap Pos = " + tap.pos);

		Ray ray = Camera.main.ScreenPointToRay(tap.pos);

		RaycastHit hit;

		if(Physics.Raycast(ray, out hit) && plant != null) {

			var randomRotation = Quaternion.Euler( 0 , Random.Range (0, 360), 0);
		
			GameObject newPlant = Instantiate(plant, hit.point, randomRotation) as GameObject;

			Debug.Log("newPlant name = " + newPlant.name);

			Plant thePlant = newPlant.GetComponent<Plant>();

			thePlant.SetPlant(atlasSpriteName, plant, "description", "variety", germinationTime, growthTime, growthRate);

			thePlant.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f);

			Gesture.onMultiTapE -= WhereToUse;

			this.quantity -= 1;

			button.UpdateButton(this.quantity);

		}
	}

And in the parent class is:

public int quantity;