The value is not reset for prefab.

Hi there.

I am facing a problems when I doing my game.

Its a defender game as the enemy hit your wall, your wall will be decreasing hp, till <=0, it destroyed.
Everything is OK, the hp will reset to 10 when each play.

But after I made my wall into a prefab (it used to be just a object on scene), the Hp is not reset to the value I set (10) but continues from the previous play, and its not destroy itself anymore when Hp is <= 0…

Beginning of 1st time play, debug.log show hp = 10,

End of 1st time play, debug.log show hp = 5

Beginning of 2nd play, debug.log show hp = 5,

End of 2nd play, debug.log show hp = 0, (and its not destroying itself)

Beginning of 3rd play, debug.log show hp = 0,

End of 3rd play, debug.log show hp = -5,

This is my code for the Wall:

using UnityEngine;
using System.Collections;

public class Wall : MonoBehaviour {


	public int castleHp = 10;

	// Use this for initialization
	void Start () 
	{
		castleHp = 10;
	}

	public void Damaging(int dmg)
	{
		Debug.Log (castleHp);
		castleHp = castleHp - dmg;
		Debug.Log (castleHp);
	}
		

	// Update is called once per frame
	void Update () 
	{
		if (castleHp <= 0)
			Destroy (gameObject);
	}
}

And this is the code for one of the enemy:

using UnityEngine;
using System.Collections;

public class EnemyAK : MonoBehaviour {
	
	public float lifeAK = 10;
	public float speed = 4.0f;
	bool shouldmove = true;
	public Wall wall;
	public int damage = 5;


	// Use this for initialization
	void Start ()
	{
		
	}
	
	void OnCollisionEnter (Collision col)
	{
		if (col.gameObject.tag == "CannonBall") 
		{
			lifeAK--;
		}

		if (col.gameObject.tag == "Walls") 
		{
			wall.Damaging (damage);
			shouldmove = false;
			Destroy (gameObject);
		}
	}



	// Update is called once per frame
	void FixedUpdate () 
	{
		//Destroy the object itself
		if (lifeAK == 0) 
		{
			Destroy (gameObject);

		}

		//walk/movement
		if(shouldmove == true)
		{
		transform.Translate(Vector3.left*Time.deltaTime*speed);
		}
	}
}

When you instance a new object it uses the data from an object in your project folders, which is is universal whereever that prefab is used Example

if i wanted to instantiate a bullet and do something to the bullet once it was instanced
you would instance using the Prefab as a reference but creating a new variable for this object to affect

Transform spawnedBullet = (Transform)Instantiate(bullet, position, rotation);

spawnedBullet is now the object that is to be affected in future so that the prefab is always the same for new instances

i Agree with skylem, if you make changes to the instantiated prefab it will directly affect the prefab as it works as a global object. Hence any changes made it it will remain.

If you want to work every time you must instantiate the prefab into another object and then try to change values, which wont affect the original prefab.

For example:

GameObject objTemp = (GameObject)Instantiate(objToInstantiate);

Here any changes made to or the values of the “objTemp” won affect the original prefab “objToInstantiate”.