Search Unity

Question Prefab Instance Missing Components

Discussion in 'Prefabs' started by PlazSoft, Jan 1, 2023.

  1. PlazSoft

    PlazSoft

    Joined:
    Dec 23, 2018
    Posts:
    6
    When I instantiate animals, the collider is not working. I attached the game instance and prefab screenshots.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SpawnManager : MonoBehaviour
    7. {
    8.     public GameObject[] animalPrefabs;
    9.     private float spawnRangeX = 25;
    10.     private float spawnPosZ = 20;
    11.      
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         InvokeRepeating("SpawnRandomAnimal", 2, 1.5f);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.     }
    23.  
    24.     void SpawnRandomAnimal()
    25.     {
    26.         int animalIndex = Random.Range(0, animalPrefabs.Length);
    27.         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
    28.         Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[0].transform.rotation);
    29.     }
    30.  
    31. }
    32.  
    33.  
     

    Attached Files:

  2. PlazSoft

    PlazSoft

    Joined:
    Dec 23, 2018
    Posts:
    6
    I fixed it, it was missing a script. I was just confused with the prefabs and I had to click to a different spot in the tree to see what is going on. Thanks!