Prefab destroyed upon instantiation
Hi everybody, first question here, hope it's a good one!
I'm creating a game where the player gets multiple weapons they can swap between. To facilitate this, I have created a base class (WeaponBase) and inheriting from that, a child class (WeaponSword).
WeaponSword looks like this (pared down to relevant code only):
public class WeaponSword : WeaponBase
{
// Use this for initialization
void Start()
{
print("Sword Start");
}
void OnDestroy()
{
Debug.Log("Sword Destroyed");
}
// Update is called once per frame
void Update()
{
Debug.Log("Sword Update");
}
}
}
I've created a Prefab which has my sword model, animations, etc, and the WeaponSword script.
I have created a GameObject in the scene to which a script is attached to handle the weapons, and this script has a public array (weapons) which stores my list of weapons. I've dragged the Prefab into the script's weapons slot in the Unity Editor.
Here is the relevant code:
public GameObject[] weapons;
private GameObject equippedWeapon;
// Use this for initialization
void Start()
{
equippedWeapon = (GameObject)Instantiate(weapons[0]);
}
void Update()
{
Debug.Log(equippedWeapon);
}
Here is where it gets tricky...
When I start the game, the weapon's model does not appear. If I pause the game and investigate the Scene view and look at the list of GameObjects, I do not see any WeaponSword objects... Looking at the console, I see some very strange behavior. The logs look like this:
Sword Start
WeaponSword(Clone) (UnityEngine.GameObject)
Sword Update
WeaponSword(Clone) (UnityEngine.GameObject)
Sword Start
Sword Update
Sword Start
Sword Destroyed
Sword Destroyed
Sword Destroyed
null
null
null
null
so what I gather from that is that... apparently multiple copies of this Prefab are instantiated (despite existing only as a Prefab and not in the scene initially, and the only call to instantiate is the one in the weapon management script which exists only once in the scene), then all of them are deleted (actually, all of them are deleted and then one more that does not apparently Start is Destroyed as well!), and from then on my weapon manager script stores no reference to the object.
I don't call Destroy anywhere in this code, and especially do not call Destroy on any WeaponSword objects.
Why is my instantiated object getting destroyed immediately?
Thank you for reading this rather long and involved question.
Are you familiar with the idea of a stack trace? If you expand the console window in Unity, you can click on each debug message to see the stack trace that led up to it. That should be very useful if you see the series of function calls that results in a sword getting destroyed.
I would pretty much guarantee that you are calling Destroy
unexpectedly, either indirectly through some bug or from some script you didn't expect.
Hi, thank you for the response! Stack trace shows only:
Sword Destroyed UnityEngine.Debug:Log(Object) Aberon.Weapon.WeaponSword:OnDestroy() (at Assets/Scripts/WeaponSword.cs:75)
So doesn't offer too much extra information.
Your answer
Follow this Question
Related Questions
What is Casting in Unity ? 1 Answer
Destroy prefabs after they have been instantiated (C#) 1 Answer
I need help about Basic Basketball Game 0 Answers
Create gameobject in other scene... 0 Answers
Find instantiated prefab by name 3 Answers