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 /
avatar image
0
Question by Snailtan · May 10, 2018 at 11:59 AM · c#array2d gamenullreferenceexception

NullReferenceExeption when trying to put GameObject into an Array

Hello! I am currently working on a 2D TopDown Survival game, and need some help.

To get the Tile GameObject for doing stuff like digging, I save all tiles of my current chunk into an array.

For example: Tile-198-7 (Numbers are x and y positions) are saved in an array by position. This Tile would be saved in myArray[198,7].

When I try to put my tile into that array, I get that Error here:

NullReferenceException: A null value was found where an object instance was required. TerrainManagerScirpt.loadTileInArray (UnityEngine.GameObject tile, Int32 xPos, Int32 yPos) (at Assets/Scripts/Terrain/TerrainManagerScirpt.cs:44) Tiles.createTile (Int32 x, Int32 y, Int32 intTileType, UnityEngine.Sprite sprite, Int32 offset) (at Assets/Scripts/Terrain/Tiles.cs:23) TerrainManagerScirpt.Start () (at Assets/Scripts/Terrain/TerrainManagerScirpt.cs:31)

Down below are my scripts. Thanks!

Tiles

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Tiles
 {
     //not used for now, Tile ID
     int tileID;
     //The Tile object
     GameObject objectTile;
     //Tile Sprite
     Sprite tileSprite;
     public Vector3 tilePos;
     TerrainManagerScirpt terrainMan;
     //Creates the TileObject. Uses position, what Tile it is, and the sprite used. (Tile type is only 
     //for reference, is not used to decide the Sprite. This is the job of the terrain Manager.
     public void createTile(int x, int y, int intTileID, Sprite sprite, int offset)
     {
         //Gets the TerrainManager
         GameObject terrainMan = GameObject.Find("TerrainManager");
         TerrainManagerScirpt terrainManager = terrainMan.GetComponent<TerrainManagerScirpt>();
         //Gives ID to Tile (for reference)
         tileID = intTileID;
         //creates the actual Tile. Tile has coordinates in its name
         objectTile = new GameObject("Tile-" + x + "-" + y);
         //sets offset
         objectTile.transform.position = new Vector3(x - offset, y - offset, 0);
 
         //Gives Position to Tile (for reference)
         tilePos = objectTile.transform.position;
 
 
 
 
         ////
         ////
         ////
         //This is where the GameObject is supposted to get saved into the array. It gives
         //The object to the loadTileArray funktion in my TerrainManager
         terrainManager.loadTileInArray(objectTile, (int)objectTile.transform.position.x, (int)objectTile.transform.position.y);
         renderTile(sprite);
 
     }
     //Renders Tile, adds SpriteRenderer and sets Sprite.
     public void renderTile(Sprite sprite)
     {
         tileSprite = sprite;
         objectTile.AddComponent<SpriteRenderer>();
         objectTile.GetComponent<SpriteRenderer>().sprite = tileSprite;
         }
 
 }


TerrainManagerScript

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TerrainManagerScirpt : MonoBehaviour {
 
     //Saves all Sprites in an array
     public Sprite[] sprites;
     //Saves tiles in an array
     public GameObject[,] mapTiles;
 
     //Sets size of Map
     private int mapSize = 20;
     private int x;
     private int y;
     //sets offset, so map gets generated "around" the player
     private int offset;
 
     public int tileType;
 
     Tiles Tile;
 
     void Start() {
         //set Offset
         offset = mapSize / 2;
         //Create TileMap
         for (int x = 0; x < mapSize; x++)
         {
             for (int y = 0; y < mapSize; y++)
             {
                 Tile = new Tiles();
                 Tile.createTile(x, y, tileType, sprites[tileType], offset);
             }
         }
     }
 
     // Update is called once per frame
     void Update() {
 
     }
     //This is used to load tiles into my array (mapTiles)
     public void loadTileInArray(GameObject tile,int xPos, int yPos)
     {
         GameObject newTile = tile;
         mapTiles[xPos, yPos] = newTile;
     }
 
     
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Snailtan · May 10, 2018 at 12:24 PM

Solved it

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

104 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

Related Questions

NullReferenceException: Array C# 1 Answer

Array items null but appear normal in inspector 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException - Maze Generator Script 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