I'm trying to spawn one random item inside an empty GameObject
. But for some reason some times it spawns 2 specific prefabs instead of one and i can't figure out where exactly the problem is.
My spawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemSpawner : MonoBehaviour
{
[SerializeField] private GameObject dogeCoindPrefab, rockCoinPrefab, slimePrefab;
[System.Obsolete]
void Start()
{
int itemNumber = Random.RandomRange(0,3);
switch (itemNumber)
{
case 0:
spawnGameObject(dogeCoindPrefab);
break;
case 1:
spawnGameObject(rockCoinPrefab);
break;
case 2:
spawnGameObject(slimePrefab);
break;
default:
spawnGameObject(dogeCoindPrefab);
break;
}
}
private void spawnGameObject(GameObject selectedGameObject)
{
Instantiate(selectedGameObject, gameObject.transform.position, Quaternion.identity);
}
}
CodePudding user response:
The code seems ok, make sure you didn't add a script to your gameObject twice. Other than that you can try to debug, and set breakpoint in spawnGameObject() to see if it's called twice, and check callstack maybe
CodePudding user response:
Your code looks ok.
Go check the script has assigned in two gameObjects.
You may try Debug.Log()
function in Start()
to check this.