- Home /
Only half of a prefab is spawning?
So I have created a spawner to spawn an enemy in my game. The enemy is a prefab consisting of two 3d triangles, with one the child object of the other. This code for spawning works fine for prefabs that only consist of one part, but when it tries to spawn this other enemy, only half of it (the child object) is spawned. Here is the spawner code: Any Ideas?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MonsterSpawner2 : MonoBehaviour {
public float delta = 1.5f;
public float speed = 2.0f;
private Vector3 startPos;
public Transform spawnpoint;
public GameObject redshell;
public int EnemySpeed = 7;
public int enemiesleft = 10;
public bool useInvoke;
public bool running;
// Use this for initialization
void Start () {
if (useInvoke)
InvokeRepeating("redspawn", 0, 3);
else
StartCoroutine(spawnred());
}
// Update is called once per frame
void Update () {
if (enemiesleft < 1)
{
Destroy(gameObject);
}
}
IEnumerator spawnred()
{
running = true;
while (running)
{
if (PlayerController.stomata > 1699)
{
enemiesleft = enemiesleft - 1;
spawnredshell();
yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
continue;
}
yield return null;
}
}
void spawnredshell()
{
var shellspawn = (GameObject)Instantiate(
redshell,
spawnpoint.position,
spawnpoint.rotation);
shellspawn.GetComponent<Rigidbody>().velocity = shellspawn.transform.up * EnemySpeed;
Destroy(shellspawn, 10.0f);
}
}
*Actually I think its the Parent Object that is only getting spawned.
Is the child of the parent GameObject also a child of that GameObject in the prefabs file? Either way, you can respawn the child object as a prefab as a child of the parent gameObject.
Sorry, im not sure what you mean. Are you saying that I need to spawn both peices of the prefab in the code? Does each peice need to be its own prefab?
Answer by OneCept-Games · Jan 02, 2018 at 08:46 PM
Drag you Prefab into your scene, and check if it is as you expect. It sounds like you have just prefabbed the child in this case.
Okay... so do I need to make the child not a prefab? how do I do this?
To test if you have created your Prefab correctly, drag it into your Scene, and see if it contains both a parent and a child.
Just to be clear, you mean that the prefab has the second part of it dragged under it so you can view it by clicking the drop down menu next to the parents name, correct? If so, yes it has a parent and a child.
Your answer
Follow this Question
Related Questions
,Getting a Spawner Manager Script to reference a Spawner 1 Answer
How do I get an object to Instantiate infront of a specific object/player? 1 Answer
SOLVED : Adding and retrieving instantiated objects from Array 1 Answer
Disfigured sprite after build. 0 Answers
How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 Answers