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 Thashrif · Aug 19, 2017 at 03:21 PM · errorobjecterror messageobject reference

[Begginer] [Solved] How to solve a Null Reference Exception

This is the error message I am getting.. I am a beginner of coding but it was actually a tutorial on youtube I followed and I can't see what error I made even though I replayed the video many times. Thanks for any help :D

NullReferenceException: Object reference not set to an instance of an object LevelManager.PlaceTile (System.String tileType, Int32 x, Int32 y, Vector3 worldStart) (at Assets/Scripts/LevelManager.cs:60) LevelManager.Createlevel () (at Assets/Scripts/LevelManager.cs:49) LevelManager.Start () (at Assets/Scripts/LevelManager.cs:23)

This is my script

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

public class LevelManager : MonoBehaviour {

 [SerializeField]
 private GameObject[] tilePrefabs; 

 [SerializeField]
 private CameraMovement cameraMovement;
 public float TileSize

 {
     get { return tilePrefabs[0].GetComponent<SpriteRenderer> ().sprite.bounds.size.x; }
 }

 // Use this for initialization
 void Start () 
 {
     Createlevel();
 }
 
 // Update is called once per frame
 void Update ()
 {
     
 }

 private void Createlevel ()
 {
     string[] mapData = ReadLevelText ();
  

         int mapX = mapData[0].ToCharArray().Length;
         int mapY = mapData.Length;

     Vector3 maxTile = Vector3.zero;

     Vector3 worldStart = Camera.main.ScreenToWorldPoint(new Vector3 (0, Screen.height));

     for(int y = 0; y < mapY; y++) 
     {
         char[] newTiles = mapData [y].ToCharArray();
         for(int x = 0; x < mapX; x++) 
         {
             maxTile = PlaceTile (newTiles[x].ToString(),x,y,worldStart);
         }
     }

     cameraMovement.SetLimits (new Vector3(maxTile.x + TileSize, maxTile.y - TileSize));
 }

 public Vector3 PlaceTile(string tileType, int x, int y, Vector3 worldStart)
 {
     int tileIndex = int.Parse (tileType);
     TileScript newTile = Instantiate (tilePrefabs[tileIndex]).GetComponent<TileScript>();

The following line is where it says the error is

     newTile.Setup(new Point(x,y), new Vector3 (worldStart.x + (TileSize * x), worldStart.y - (TileSize * y), 0));
     return newTile.transform.position;
 }

 
 private string[] ReadLevelText()
 {
     TextAsset bindData = Resources.Load ("Level") as TextAsset;
     string data = bindData.text.Replace(Environment.NewLine,string.Empty);

     return data.Split('-');
 }

}

Comment
Add comment · Show 1
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 brendanwoodell · Apr 17, 2018 at 03:35 AM 0
Share

Dang I was following the same tutorial and ran into the same problem. Was hoping you'd have found an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Apr 17, 2018 at 08:19 AM

Goo day guys.

This error, is the most common error at Unity.

It says : "A variable is not assigned when trying to execute the line of code number "number" at Script "scriptName"".

So.. You must discover 3 things:


1- What line of code is giving the error: Look at the error message, in our case is LevelManager script, at line 23 (CreateLevel()) and line 60 (which is the CreateLevel). (As you didnt pasted correctly all the script so i can't know what line is line 60


2- Discover what variable is not assigned (is still null) when trying to execute that line. To discover this, you can or debug the code while runnign, or put a Debug.Log message with all the variables just before the problematic line.

 Debug.Log(variable);

To see in the console the value of all variables, and one of them will be null.


3- Now yo know what variable is returning null, by understanding your script, you must discover why is not assigned yet! (Maybe you need to assign it via inspector, or maybe by code...)

Thats the basic 3 stops to solve ALL null reference errors.



I helped, Accept the answer and close the question!

Bye!

Comment
Add comment · Share
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

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

136 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 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 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

Array of dictionaries: "Object reference not set to an instance of an object" 0 Answers

PLEASE HELP!! Object reference not set to an instance of an object at ChangeMasterVolume.Update 1 Answer

Getting an error: Assertion failed on expression: 'SUCCEEDED(hr)' 5 Answers

Error That Doesn't Affect My Game Is Spammed Every Frame. 1 Answer

I have a problem with my code please help 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