Noobie Noobie - 3 years ago 85
C# Question

Why does the GameObject not append to the List?

I am currently working on a game prototype, this game would require to
spawn some GameObjects which I call "stars", all good with
instantiating, but when I try to delete them when there are to many
around it does not work, I am trying to put all instantiated
GameObjects in a list and then delete the last object from it when
the new one was instantiated. As you can see from the code, when 3
objects are spawned the script should delete one from the beginning
and spawn a new one in the same time.

Now, the problem is that I don't know what am I missing here, the code does not work and I don't know why. Please help me. Thank you!

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnStar : MonoBehaviour {

public float addedSpeed = -200f;

private GameObject spawned;
private float randomX;
public GameObject starToSpawn;
private List<GameObject> activeGO;


void Start ()
{
activeGO = new List<GameObject> ();
Invoke ("InstantiateStar", 2f);
}

// Update is called once per frame
void FixedUpdate ()
{
GetComponent<Rigidbody>().AddForce( new Vector3( 0f, addedSpeed, 0f));
}

void InstantiateStar ()
{
randomX = Random.Range (-3f, 3f);
GameObject GO;
GO = Instantiate (starToSpawn, new Vector3(randomX, 5f, 0f), transform.rotation) as GameObject;
activeGO.Add (GO);
if ( activeGO[0].transform.position.y < -2f)
{
DeleteActiveGO ();
}
}
void DeleteActiveGO ()
{
Destroy (activeGO [0]);
activeGO.RemoveAt (0);
}
}


I am back with an update. The problem was that I was trying to do 2 thing in one script...short story: To solve the problem I created an empty object in the scene, divided my script in 2 separated scripts, one that makes the spawned object move faster and one that is spawning the object, I put the script that moves the object on to the object that will be spawned and The other script on the empty GameObject that will spawn the "Stars" and it worked like a charm.
Thank you for all your answers!

here are the final scripts:


Spawning script:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnStars : MonoBehaviour {

public GameObject[] starsToSpawn;

private List<GameObject> spawnedStars;

private float randomX;

void Start ()
{
spawnedStars = new List<GameObject> ();
InvokeRepeating ("SpawnStar", 0f, 3f);
}
void SpawnStar ()
{
randomX = Random.Range (-3, 3);
GameObject GO;
GO = Instantiate ( starsToSpawn[0], new Vector3 (randomX, 5f, 0f), transform.rotation);

spawnedStars.Add (GO);

if (spawnedStars.Count > 2 )
{
Destroy (spawnedStars [0]);
spawnedStars.RemoveAt (0);
}
}
}



Moving script:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveStar : MonoBehaviour {

public float acceleration = -5f;

void FixedUpdate ()
{
GetComponent<Rigidbody>().AddForce( new Vector3( 0f, acceleration,0f));
}
}

Answer Source

Your code Invoke ("InstantiateStar", 2f); only call once.

You can change to InvokeRepeating("InstantiateStar",2f,2f );

The Code GetComponent<Rigidbody>().AddForce( new Vector3( 0f, addedSpeed, 0f)); seem like should attach to the Generate Gameobject.

Also note your delete condition.

good luck.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download