There is a problem. I have a Spawner that instantiate Units from prefabs as GameObjects. After spawning, all units are stored in List. After certain of time I need to destroy units (not fixed time and not time-based at all, but only at will), but the script Destroy (gameObject) not working. Here is the sample of my code:
//Spawner code fragment
GameObject Loco1;
Loco1 = Instantiate (TestLoco, transform.position, transform.rotation) as GameObject;
UM.Units.Add(Loco1.transform.GetComponent<BasicUnitScript>().thisUnit); //adding new unit to list in UnitManager
//Unit code fragment
void OnAwake ()
{
UM = GameObject.Find ("Camera").GetComponent<UnitManager> ();
thisUnit.gUnit = gameObject;
thisUnit = UM.UnitReturn (this.gameObject);
//UnitManager returns to object reference to itself in the List
public class UnitManager : MonoBehaviour {
public List<Unit> Units = new List<Unit> ();
public Unit UnitReturn(GameObject gUnit)
{
for(int i = 0; Units.Count > 0; i++)
{
if(gUnit == Units*.gUnit)*
-
{*
_ return Units*;_
_ }_
_ }_
_ return null;_
_ }_
_}*_
//and again Unit’s code sample
* public void Sell ()*
* {*
* arrowMarked = false;*
* if (isSelected)*
* {*
* UM.UnitsSelected.Remove (thisUnit);*
* }*
* UM.Units.Remove (thisUnit);*
* Destroy (gameObject);*
* }*
What I can’t understand, is that: at the same tame another specially instantiated object (3D animated arrow pointing selected unit) is destroying normally (it destroyed when unit deselected and instantiated on new select).