- Home /
Spawn different gameObjects without using a prefab? | Spawned gameObjects don't enable restartPanel
Hello! A desperate beginner here.
I am making a game where different gameObjects are spawned and you have to drag them to prevent them from collision. Upon collision a restartPanel is enabled. This system works fine with gameObjects that are already placed in the game (hierarchy), but the collision of spawned gameObjects does not enable the restartPanel (because dragging a scene on a prefab is not possible, as I just learned).
Is there a way to spawn different gameObjects without using a prefab?
For spawning I used this code with a prefab and multiple sprites
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HumanSpawn : MonoBehaviour
{
public GameObject humanPrefab;
public Sprite[] humanSprites;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.1f;
void Update()
{
if (Time.time > nextSpawn){
nextSpawn= Time.time + spawnRate;
randX = Random.Range (-8.4f, 8.4f);
whereToSpawn = new Vector2 (randX, transform.position.x);
Instantiate(humanPrefab, whereToSpawn, Quaternion.identity);
}
}
}
Answer by Hellium · May 31, 2020 at 09:55 PM
Prefabs is the way to go. If you want to reference your restartPanel
in a script attached to your prefabs, your best option is to inject the reference when the prefab gets instantiated.
Supposing you have a script called Human
attached to your prefab, you can do something like this:
public class Human : MonoBehaviour
{
public GameObject RestartPanel { get; set; }
private void OnCollisionEnter(Collision collision)
{
RestartPanel.SetActive(true);
}
}
Then, in your spawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HumanSpawn : MonoBehaviour
{
// Drag & drop the restartPanel in the inspector
// You should be able to since `HumanSpawn` is most likely in your scene already
public GameObject restartPanel;
public GameObject humanPrefab;
public Sprite[] humanSprites;
public float spawnRate = 2f;
float nextSpawn = 0.1f;
void Update()
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
float randX = Random.Range (-8.4f, 8.4f);
Vector2 spawnPosition = new Vector2 (randX, transform.position.x); // Don't you mean transform.position.y?
GameObject human = Instantiate(humanPrefab, spawnPosition, Quaternion.identity);
human.GetComponent<Human>().RestartPanel = restartPanel;
}
}
}
Yes, thank you so much for taking the time to type this out! Referencing the restartPanel in the prefab did the trick! :) For some reason, after the changes in the script, only one of the sprites is spawning. Would using a switch statement fix this?
I am not sure you would have different sprites with the code you have provided. Try something like this:
void Update()
{
if (Time.time > nextSpawn)
{
SpawnHuman();
nextSpawn = Time.time + spawnRate;
}
}
void SpawnHuman()
{
float randX = Random.Range (-8.4f, 8.4f);
Vector2 spawnPosition = new Vector2 (randX, transform.position.x); // Don't you mean transform.position.y?
GameObject human = Instantiate(humanPrefab, spawnPosition, Quaternion.identity);
human.GetComponent<Human>().RestartPanel = restartPanel;
human.GetComponent<SpriteRenderer>().sprite = humanSprites[Random.Range(0, humanSprites.length)];
}
Your answer
Follow this Question
Related Questions
Spawn gap in unity? 0 Answers
Error: Instantiated Enemies don't get hit 2 Answers
Destroy a GameObject 2 Answers
RayCast Collision between 2 game objects (of the same prefab) 1 Answer
Coin collecting with prefabs 2 Answers