Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ezhilartworks · Apr 06, 2016 at 10:41 AM · nullreferenceexception

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

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ezhilartworks · Apr 06, 2016 at 11:21 AM 0
Share

i followed this tutorial

https://www.youtube.com/watch?v=F0GkcgdXO$$anonymous$$Y

avatar image Jessespike · Apr 06, 2016 at 04:42 PM 0
Share

Are there GameObjects named "gLeft, gRight, g$$anonymous$$iddle, gBlank" in the scene? And do they have a child?

avatar image ezhilartworks Jessespike · Apr 06, 2016 at 06:35 PM 0
Share

@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 italt text

intial-debug.png (200.9 kB)
avatar image Jessespike · Apr 06, 2016 at 07:33 PM 0
Share

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.

avatar image ezhilartworks Jessespike · Apr 06, 2016 at 08:07 PM 0
Share

it gives me an error cs0029

Assets/scripts/levelCreator.cs(77,25): error CS0029: Cannot implicitly convert type UnityEngine.Transform' to UnityEngine.GameObject'

avatar image Jessespike ezhilartworks · Apr 06, 2016 at 08:57 PM 0
Share

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.

Show more comments
avatar image Jessespike · Apr 07, 2016 at 07:22 AM 0
Share

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.

avatar image ezhilartworks Jessespike · Apr 07, 2016 at 09:12 AM 0
Share

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 :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

55 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges