All instances of a an object are being updated at once instead of the object in question.

Hi all,

First time posting a question and I’m new to unity so bear with me.

I’ve run into a problem with one of my base building scripts. The way it is supposed to work is like this; the player walks onto the buildsite and selects a building to construct. Currently by either pressing ‘1’ or ‘2’. Then once that is complete the option to upgrade becomes available. The snag at the moment though is that if the player has enough resources then all instances of that building are being updated at once.

What I actually want is that only that one buildsite is updated.

The build code goes like this:

`using UnityEngine;
using System.Collections;

public class constructionScript : MonoBehaviour {
		

		private resourceGUI material;
		private bool canBuild = false;
		public Object Building;
	    public Object Building2;
		
		bool toggleGUI;

		void  Start (){
		material = GameObject.Find("Player").GetComponent<resourceGUI>();
		}
		
		void  OnTriggerEnter (Collider other){
			toggleGUI = true;
			canBuild = true;
		}
		
		void  OnTriggerExit ( Collider other){
			toggleGUI = false;
		}
		
		void  OnGUI (){
			if (toggleGUI == true)
				GUI.Label (new Rect(500, 300, 300, 40), "Press '1' to build Barracks.

Press ‘2’ to build Forge.");

		}

		void  Update (){
		if(canBuild == true)
		{
			if(Input.GetKeyDown("1"))
			{
				if(material.currentWood >= 1 && material.currentMetal >= 1 && material.currentStone >= 1)
				{
					Instantiate(Building,transform.position,transform.rotation);

					material.currentWood--;
					material.currentMetal--;
					material.currentStone--;

					Destroy(gameObject);

				}
			}

			if(Input.GetKeyDown("2"))
			{
				if(material.currentWood >= 1 && material.currentMetal >= 1 && material.currentStone >= 1)
				{
					Instantiate(Building2,transform.position,transform.rotation);

				//Set materials to zero
				material.currentWood--;
				material.currentMetal--;
				material.currentStone--;
				
				Destroy(gameObject);
				}
			}
		}

	}
}

At the moment this allows me to build individual buildings on each buildsite even if I have more than enough resources to update all the buildsites at once. The problem seems to be in my upgrade script.

This is the script that is attached to the prefabs that are called in the above script:

using UnityEngine;
using System.Collections;

public class upgradeScript : MonoBehaviour {
	
	
	private resourceGUI material;
	private constructionScript Building;
	private bool canBuild = false;
	public Object buildingUpgrade;
	
	bool toggleGUI;
	
	void  Start (){
		material = GameObject.Find("Player").GetComponent<resourceGUI>();
	}
	
	void  OnTriggerEnter (Collider other){
		toggleGUI = true;
		canBuild = true;
	}
	
	void  OnTriggerExit ( Collider other){
		toggleGUI = false;
	}
	
	void  OnGUI (){
		if (toggleGUI == true)
			GUI.Label (new Rect(500, 300, 300, 40), "Press 'U' to upgrade building.");
		
	}
	
	void  Update (){
		if(canBuild == true)
		{
			if(Input.GetKeyDown("u"))
			{
				if(material.currentWood >= 2 && material.currentMetal >= 2 && material.currentStone >= 2)
				{
					Instantiate(buildingUpgrade,transform.position,transform.rotation);

					material.currentWood-=2;
					material.currentMetal-=2;
					material.currentStone-=2;

					Destroy(transform.parent.gameObject);
				}
			}

		}
		
	}
}

Could the problem be that the scripts are attached to prefabs and are all clones?

Any help on this is greatly appreciated as is any criticism about the code in general. I’ve next to no knowledge of both unity and C# but I have gone through a few of the tutorials and played around with some of the scripts that can be found here and there so this is all a learning experience to me.

Cheers!

You never set the canBuild variable to false, so if the player touched the building once it will stay selected for upgrade.

In OnTriggerExit add canBuild = false;