- Home /
Error while spawning multiple objects
So I am new to Unity and am trying to spawn multiple objects from bottom going up.I have created a startpoint object to destroy the object if it enters and detect multiple objects by its tag and endpoint to spawn my 2d objects from there. Also the first two objects randomly and then stop spawning.This is my code....thank you.
//start using System.Collections; using System.Collections.Generic; using UnityEngine;
public class controller : MonoBehaviour { public GameObject obs1; public GameObject obs2; public GameObject obs3; public GameObject obs4; public GameObject obs5; public GameObject obs6; public GameObject obs7; public GameObject startpost;
int counter = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
counter = Random.Range(1, 8);
if (collision.gameObject.tag == "obstacle")
{
if (counter == 1)
{
Instantiate(obs1, new Vector2(startpost.transform.position.x, obs1.transform.position.y), Quaternion.identity);
}
else
if (counter == 2)
{
Instantiate(obs2, new Vector2(startpost.transform.position.x, obs2.transform.position.y), Quaternion.identity);
}
else
if (counter == 3)
{
Instantiate(obs3, new Vector2(startpost.transform.position.x, obs3.transform.position.y), Quaternion.identity);
}
else
if (counter == 4)
{
Instantiate(obs4, new Vector2(startpost.transform.position.x, obs4.transform.position.y), Quaternion.identity);
}
else
if (counter == 5)
{
Instantiate(obs5, new Vector2(startpost.transform.position.x, obs5.transform.position.y), Quaternion.identity);
}
else
if (counter == 6)
{
Instantiate(obs6, new Vector2(startpost.transform.position.x, obs6.transform.position.y), Quaternion.identity);
}
else
if (counter == 7)
{
Instantiate(obs7, new Vector2(startpost.transform.position.x, obs7.transform.position.y), Quaternion.identity);
}
Destroy(collision.gameObject);
}
}
} //end
//Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. controller.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/controller.cs:50)
Answer by pyramidhead01 · Jul 16, 2019 at 01:20 PM
What you want to do is pool your objects, and then set them active and inactive.
The solution I'm giving you isn't tested, I pass the time at work with this, so if you run into issues, let me know.
using System;
using Unity.Engine.UI;
using System.Collections.Generic;
public class Controller : MonoBehaviour
{
//Instead of making a bunch of GameObject variables, create a list, and set it publicly in inspector
public List<GameObject> objects = new List<GameObject>();
int counter;
bool startSpawning;
public GameObject startpost;
private void Start()
{
foreach(GameObject obj in objects)
{
Instantiate(obj, transform.position, Quaternion.Identity);
obj.SetActive(false);
}
}
private void Update()
{
if(startSpawning == true)
{
Spawn();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "obstacle")
{
startSpawning = true;
}
}
private void Spawn()
{
counter = Random.Range(0, objects.Count);
objects[counter].SetActive(true);
objects[counter].transform.position = (startpost.transform.position.x, objects[counter].transform.position.y);
objects.Remove(counter);
if(objects.Count < 1)
{
Destroy(collision.gameObject);
startSpawning = false;
}
}
}
The above solution you have to add all the game objects to the list publicly in the inspector. This should also spawn all the objects in a random order, I'm not 100% sure if that works (or works the way you want) but it should spawn everything before it destroys the colliding object in a much more elegant way than you had before.
For more seasoned coders, yes I'm aware a while loop (or maybe even for loop) would also work, but just trying to keep this solution simple.
Again, if it doesn't work, let me know and I'll try again; it also is a good idea not to make sure I made spelling mistakes or something like that first because I don't have a compiler, have not tested this, and I might have a typo in there that is causing an error, so please make sure everything that is underlined red on your end isn't a typo from me.
Thank you very much for that long code! Sorry to disturb you but I am getting these two errors around line 44 and 45 tried my best couldn't solve them...
Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type '(float x, float y)' to 'UnityEngine.Vector3' Assembly-CSharp 44 Active
Error CS1503 Argument 1: cannot convert from 'int' to 'UnityEngine.GameObject' Assembly-CSharp 45 Active