Search Unity

Question HELP PLEASE. NEW OBJECTS WITHOUT COMPONENTS

Discussion in 'Scripting' started by varnik_unity, Mar 16, 2023.

  1. varnik_unity

    varnik_unity

    Joined:
    Mar 16, 2023
    Posts:
    3
    URGENTLY!
    Someone help, i made 2d runner game, spawner isn't working. I used 3 spawn variants, but new objects spawns wisout components, i need this project tomorrow . Help please


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     public GameObject[] EnemyVariants;
    8.  
    9.     private float timeBtwSpawn;
    10.     public float startTimeBtwSpawn;
    11.     public float decreaseTime;
    12.     public float minTime = 0.65f;
    13.  
    14.     private void Update()
    15.     {
    16.         timeBtwSpawn -= Time.deltaTime;
    17.         if (timeBtwSpawn <= 0)
    18.         {
    19.             int rand = Random.Range(0, EnemyVariants.Length);
    20.             Instantiate(EnemyVariants[rand], transform.position, Quaternion.identity);
    21.             timeBtwSpawn = startTimeBtwSpawn;
    22.             if (startTimeBtwSpawn > minTime)
    23.             {
    24.                 startTimeBtwSpawn -= decreaseTime;
    25.             }
    26.         else
    27.         {
    28.             timeBtwSpawn -= Time.deltaTime;
    29.         }
    30.         }
    31.     }
    32. }
     
    Last edited: Mar 16, 2023
  2. Deleted User

    Deleted User

    Guest

    did you check that the enemy variants you have have components?
     
  3. varnik_unity

    varnik_unity

    Joined:
    Mar 16, 2023
    Posts:
    3
  4. Deleted User

    Deleted User

    Guest

    As long as
    EnemyVariants[rand]
    has components, then the instantiated objects should also have components. By the way, you should put your code in a code block, don't put it in raw text in the forums.

    What happens when you do
    Code (CSharp):
    1. var components = EnemyVariants[rand].GetComponents<MonoBehaviour>();
    2.  
    3. Debug.Log($"Found {components.Length} components");
    4. foreach (var component in components)
    5. {
    6.   Debug.Log(component.GetType().FullName);
    7. }
     
  5. varnik_unity

    varnik_unity

    Joined:
    Mar 16, 2023
    Posts:
    3
    nothing (
     
  6. Deleted User

    Deleted User

    Guest

    Then there is a problem with the gameobjects you're assigning to
    EnemyVariants[rand]
    in the inspector.