null reference exception
hey i am following a simple runner tutorial but i get this exception and i don't know why?
NullReferenceException: Object reference not set to an instance of an object levelCreator.setTile (System.String type) (at Assets/scripts/levelCreator.cs:92) levelCreator.fillScence () (at Assets/scripts/levelCreator.cs:64) levelCreator.Start () (at Assets/scripts/levelCreator.cs:44)
and my code is
public class levelCreator : MonoBehaviour {
// Use this for initialization
private GameObject tilePos;
private float startUpPosY;
private const float tileWidth = 1.25f;
private int heightLevel = 0;
private GameObject tmpTile;
private GameObject collectedTiles;
private GameObject gameLayer;
private GameObject bgLayer;
void Start () {
gameLayer = GameObject.Find("gameLayer");
bgLayer = GameObject.Find("backgorundLayer");
collectedTiles = GameObject.Find("tiles");
for(int i = 0; i<21; i++){
GameObject tmpg1 = Instantiate(Resources.Load("ground_left", typeof(GameObject))) as GameObject;
tmpg1.transform.parent = collectedTiles.transform.FindChild("gLeft").transform;
GameObject tmpG2 = Instantiate(Resources.Load("ground_middle", typeof(GameObject))) as GameObject;
tmpG2.transform.parent = collectedTiles.transform.FindChild("gMiddle").transform;
GameObject tmpG3 = Instantiate(Resources.Load("ground_right", typeof(GameObject))) as GameObject;
tmpG3.transform.parent = collectedTiles.transform.FindChild("gRight").transform;
GameObject tmpG4 = Instantiate(Resources.Load("blank", typeof(GameObject))) as GameObject;
tmpG4.transform.parent = collectedTiles.transform.FindChild("gBlank").transform;
}
collectedTiles.transform.position = new Vector2 (-60.0f,-20.0f);
tilePos = GameObject.Find("startTilePosition");
startUpPosY = tilePos.transform.position.y;
fillScence();
}
// Update is called once per frame
void fixedUpdate()
{
}
private void fillScence()
{
for(int i=0; i<15; i++)
{
setTile("middle:");
}
setTile("right");
}
public void setTile(string type)
{
switch (type) {
case "left":
tmpTile = collectedTiles.transform.FindChild("gLeft").transform.GetChild(0).gameObject;
break;
case "right":
tmpTile = collectedTiles.transform.FindChild("gRight").transform.GetChild(0).gameObject;
break;
case "middle":
tmpTile = collectedTiles.transform.FindChild("gMiddle").transform.GetChild(0).gameObject;
break;
case "blank":
tmpTile = collectedTiles.transform.FindChild("gBlank").transform.GetChild(0).gameObject;
break;
}
tmpTile.transform.parent = gameLayer.transform;
tmpTile.transform.position = new Vector2 (tilePos.transform.position.x+(tileWidth), startUpPosY + (heightLevel * tileWidth));
tilePos = tmpTile;
}
void Update () {
}
}
thanks in advance
i followed this tutorial
Are there GameObjects named "gLeft, gRight, g$$anonymous$$iddle, gBlank" in the scene? And do they have a child?
@jessespike thank you for replying
yes the GameObjects are in the scene but they are a child of tiles and they do not have a child i have attached a screenshot of it
How can the script find a child if there are no children? That's probably the problem. Update the code so tmpTile can be assigned a GameObject.
tmpTile = collectedTiles.transform.FindChild("gLeft").gameObject;//.transform.GetChild(0).gameObject;
or add an empty GameObject to the tiles to be a child.
it gives me an error cs0029
Assets/scripts/levelCreator.cs(77,25): error CS0029: Cannot implicitly convert type UnityEngine.Transform' to
UnityEngine.GameObject'
I don't know what line that is. The line numbers on the script you posted are wrong because you didn't post the entire script. Anyway, just return a gameObject ins$$anonymous$$d. Add ".gameObject" to the end of the line that is throwing that error.
There's a bigger problem going on here. The tiles objects should have children being instantiated to them, so ignoring that problem won't help.
tmpTile = collectedTiles.transform.FindChild("gLeft").gameObject;//.transform.GetChild(0).gameObject;
This isn't a good thing, you won't be able to use the parent as a a collected tile. You should revert this back to what you had before. I shouldn't have suggested this, this will just make the immediate error go away, but it doesn't fix the problem.
NullReference means something wasn't assigned. I don't know why you're getting that error. Sorry to mislead you. I think the tutorial isn't explaining things very well. He shows off alot of code, but doesn't explain how the scene or prefabs are setup. I didn't really watch all of it, so maybe I'm wrong. I would suggest rewatching it, in case you missed something. Or try downloading the project and compare it to yours.
yeah that is true i wish the tutorial explained a bit in depth about setting things up i will re do it later actually i am trying to create an endless runner so i will try another approach if not this
anyway thanks for your support :)
Your answer
Follow this Question
Related Questions
NullReferenceException when using gameObject.SetActive(false); 0 Answers
Help needed finding NullReferenceException 1 Answer
So I know this has been asked before but, I feel this only applies to my problem. 2 Answers
Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers
NullReference.Exveption: Object reference not set to an instance of an object 1 Answer