Question by
arvanitakis95 · Jun 25, 2018 at 07:41 PM ·
c#instantiatenullreferenceexceptioninstantiate prefab
Instantiation Problem
Hello guys. I am currently facing a problem with instantiating a prefab. First I am instantiating the prefab from an array. Then the method SpawnCoin accepts an object[] and instantiates the same prefab. This is the code:
public static GameObject CoinPrefab
public static GameObject[] SpawnedCoins = new GameObject[7];
void Start ()
{
object[,] coinsfromarray = Coins.GetCoins();
for (int i = 1; i < 6; i++)
{
SpawnedCoins [i] = Instantiate (CoinPrefab, (Vector3)coinsfromarray [i, 1], CoinPrefab.transform.rotation);
}
}
public static void SpawnCoin(object[] anothercoin)
{
SpawnedCoins[6] = Instantiate(CoinPrefab, (Vector3)anothercoin[1], CoinPrefab.transform.rotation);
}
}
The first coins are instantiated as they are supposed to. However, when the SpawnCoin method is called, I get this error: NullReferenceException: Object reference not set to an instance of an object CoinSpawner.SpawnCoin
I have used Debug.Log and anothercoin[] is not null. I have also tried using a new GameObject instead of SpawnedCoins[6]. I can't get it to work. Any thoughts?
Comment