ArgumentException: The Object you want to instantiate is null.

Helloo so i am making android endless runner game i use this script to make endless road. unity says that ArgumentException: The Object you want to instantiate is null. i cant find out how to fix it and also it stops moving my object. prefab is just assigned to this prefab [126846-capture.png*|126846]Please help me what to do
Here’s the script :

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RoadController : MonoBehaviour
public List<GameObject> safeRoads;
public List<GameObject> roadBlocks;
private List<GameObject> activeRoads;

public Text txt;

private float roadLength = 60f;
private float spawnPosZ = 0.0f;
private float safeZone = 120f;

private int AMOUNT_ROADS_ON_SCREEN = 4;
private int lastPrefabIndex = 0;

public Transform playerTransform;

// Use this for initialization
void Start()
{
    activeRoads = new List<GameObject>();
    SpawnSafeRoads();
}

private void DestroyRoad()
{
    Destroy(activeRoads[0]);
    activeRoads.RemoveAt(0);
}

// Update is called once per frame
void Update()
{
    if (playerTransform.position.z - safeZone > (spawnPosZ - AMOUNT_ROADS_ON_SCREEN * roadLength))
    {
        SpawnRandomRoad();
        DestroyRoad();
    }
}

private void SpawnRandomRoad()
{
    GameObject _roadClone;
    _roadClone = Instantiate(roadBlocks[RandomPrefabIndex()]) as GameObject;
    _roadClone.transform.SetParent(transform);
    _roadClone.transform.position = Vector3.forward * spawnPosZ;
    spawnPosZ += roadLength;
    activeRoads.Add(_roadClone);
}

private void SpawnSafeRoads()
{
    GameObject roadBlock;

    for (int i = 0; i < safeRoads.Count; i++)
    {
        roadBlock = Instantiate(safeRoads*) as GameObject;*

roadBlock.transform.SetParent(transform);
roadBlock.transform.position = Vector3.forward * spawnPosZ;
spawnPosZ += roadLength;
activeRoads.Add(roadBlock);
}
}
private int RandomPrefabIndex()
{
if (roadBlocks.Count <= 1)
{
return 0;
}
int randomIndex = lastPrefabIndex;
while (randomIndex == lastPrefabIndex)
{
randomIndex = UnityEngine.Random.Range(0, roadBlocks.Count);
}
lastPrefabIndex = randomIndex;
// Debug.Log(randomIndex);
txt.text = "Index
" + randomIndex;

return randomIndex;
}
}
*

Hello there,

Try double-clicking the erro in the console. It will give you the line where the error occurs.

Also, “Txt” doesn’t seem to be assigned, yet you’re trying to set its text component. Maybe that’s the error?

Remember, the more info you can give us, the easier it’ll be to find a solution.


Hope that helps!

Cheers,

~LegendBacon