- Home /
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);
}
}
}
Are you reloading the scene between games? Or is it just a soft reset?
I press the "Play" button in unity. Then press the "Pause" button, then the "Play" button again.
pausing the game wouldn't cause any changes in the scene so im a little confused as to your question.
Answer by KaushikRahul · Apr 11, 2016 at 01:27 PM
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".
That is 100% correct, I just want to add that this has nothing to do with "prefabs". You can instantiate any object in Unity - be it self created or a prefab (which is really just a serialized version of an object).
Answer by skylem · Apr 11, 2016 at 07:33 AM
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
But before I made the wall into a prefab, It works well, able to reset the value for each pause and play...
is the reference on your script set to a gameobject in the scene or within your project folders ?
Your answer
Follow this Question
Related Questions
Inspector value resets on prefabs 1 Answer
Instantiate issues 1 Answer
How to reset a prefab used in object pool ? 2 Answers
Making a reset function for arrayed objects 2 Answers
Unity setting SerializedProperty.prefabOverride incorrectly 2 Answers