i have a problem with my unity code and i can't get it why it doesn't work. I am kinda new to unity, just starting to learn it. I have 4 prefabs with different name in the project tree, and i want a prefab to spawn every second, but i want it to be randomized without using "if"'s, so i tried to save the prefabs names in an array and then Instantiate the GameObject that has the same name with the value of the array. When i run my script in Unity it says that the object i want to instantiate is null, i tried to find an answer on the web, but i found nothing. Can you help me?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawnnoif : MonoBehaviour
{
public GameObject cub;
public GameObject capsula;
public GameObject cilindru;
public GameObject sfera;
public int x;
public GameObject paleta;
public float delta;
public string[] a = { "cub", "capsula", "cilindru", "sfera" };
void Start()
{
Vector3 position = new Vector3(UnityEngine.Random.Range(-1.88f, 2.1f), 1, UnityEngine.Random.Range(-7.81f, -3.1f));
x = UnityEngine.Random.Range(0, 3);
Instantiate(GameObject.Find(a[x]), position, Quaternion.identity);
}
IEnumerator Spawn()
{
while (true)
{
Vector3 position = new Vector3(UnityEngine.Random.Range(-1.88f, 2.1f), 1, UnityEngine.Random.Range(-7.81f, -3.1f));
x = UnityEngine.Random.Range(0, 3);
Instantiate(GameObject.Find(a[x]), position, Quaternion.identity);
yield return new WaitForSeconds(1.0f);
}
}
}
GameObject.Find(a[x])
returns something? - krobelusmeetsyndraFind
has nothing to do with tags but only with the object names - derHugo