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 ShibaIntel · Mar 16, 2016 at 04:47 PM · scripting problemgameobjectnullreferenceexception

Help Me on Storing GameObject!!!

I was trying to make a Game about Collapse game thingy as a school Project but then it comes to an error

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour 
 {
     
     public GameObject[] Block; // Takes all the tagged Colour Block
     public GameObject[][] BlockCounter; // the Spawned GameObject are stored here
     public Vector3[][] BlockPosition; // The Position of the Block in float
     
     // Use this for initialization
     void Start () 
     {
         Block = GameObject.FindGameObjectsWithTag("Block");
         int number=0;
         for (int x=0 ; x<10 ; x++)
         {
          number = Random.Range(0,3);
          BlockCounter[0][x]=Block[number];
          Instantiate (BlockCounter[0][x], new Vector3 (0,0,0) , transform.rotation);
         }
         
     }
     
     
     // Update is called once per frame
     void Update () 
     {
         
     }
 }


i have 4 GameObject that is tagged "Block" and i wanna to copy it to the array multiple times with different position (so i can access another script in each of the duplicated block individually and be able to modify it) and randomly so the block would shuffle the colour but the error its showing is :

NullReferenceException: Object reference not set to an instance of an object Spawner.Start () (at Assets/Script/Spawner.cs:19)

Please help...desperately new Unity Student is dying ! im really confused, forgive me if this is a basic wrong.

Comment
Add comment · Show 4
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 ComradeVanti · Mar 16, 2016 at 05:32 PM 0
Share

Ok I really dont get what you are trying to achieve here xD

Could you please give a more detailed explanation of the situation :) You keep mentioning colors but I dont see them in your script... Also if you only have 4 blocks why would you have them have multiple positions... One object can only be at one place...

As I said: $$anonymous$$ore details please :)

avatar image ShibaIntel ComradeVanti · Mar 17, 2016 at 01:02 AM 0
Share

Sorry about that , please Look at Positive7 Reply, i really had trouble explaining the whole idea, i hope that clear the confusion.

avatar image Positive7 · Mar 16, 2016 at 06:26 PM 0
Share

As Brullix3000 mentioned some more info would be nice. I don't think this will solve your problem, but have a look at this:

 using UnityEngine;
 
 public class Spawner : $$anonymous$$onoBehaviour
 {
 
     public GameObject[] Block; // Takes all the tagged Colour Block
     public GameObject[][] BlockCounter; // the Spawned GameObject are stored here
     public Vector3[][] BlockPosition; // The Position of the Block in float
     public int number = 0;
     int objNum = 10;
 
     void Start()
     {
         Block = GameObject.FindGameObjectsWithTag("Player");
 
         BlockCounter = new GameObject[Block.Length][];
         for (int x = 0; x < objNum; x++)
         {
 
             BlockCounter[0] = new GameObject[objNum];
             number = Random.Range(0, 3);
             BlockCounter[0][x] = Block[number];
 
             Instantiate(BlockCounter[0][x], new Vector3(0, 0, 0), transform.rotation);
         }
     }
 }
avatar image ShibaIntel Positive7 · Mar 17, 2016 at 12:59 AM 0
Share

Ah i get a bit of trouble of explaining thing, my bad. let me try to rephrase it then , this is what i learned so far: 1.First off, i try to make Block as a Array to get my gameobject in the Scene with the Tag "Block", supposedly i have 4 of them in the scene. 2.then using the "number" Variable, i random the value from 0-3, that will be use to copy the the Block into the Block Counter 3. like Tetris/Collapse/Bejewelled Game type, i imagine them to have 2D array, so BlockCounter act as the 2D Array that store the duplicated block from the Block[]. It's why

number = Random.Range(0,3); BlockCounter[0][x]=Block[number]; (the Block [0][x] its just a test to see if the first array can be copied and instantiated, which turn out failed)

Reason why i want to access each of the Instantiated object? is this:

 using UnityEngine;
 using System.Collections;
 
 public class BlockProperties : $$anonymous$$onoBehaviour 
 {
     public int PositionX;
     public int PositionY;
     public int Colour;
     
     void start()
     {
         
     }
     
     void PositionDecider(int X, int Y, int Colour)
     {
         this.PositionX = X;
         this.PositionY = Y;
         this.Colour = Colour;
     }
     
     int GetX()
     {
         return PositionX;
     }
     int GetY()
     {
         return PositionY;
     }
     int GetColour()
     {
         return Colour;
     }
 
 }

so each of the object that is Instantiated can store Position Value in the Array so when i destroy the block, the block can detect where he was on the array can search the surrounding for another same Block Colour (By comparing the Colour Int with the Left/right/up/down block in the BlockCounter[][]).

So after that i : 1. Instantiate the BlockCounter[0][x] to see if i can Instantiate it and the object was indeed copied to the BlockCounter, but it turns out, doesnt.

The Image of the game (Still messy): alt text

Im sorry for any unprofessional Question or explaining, i hope this clears out the confusion, just started this.

q-unity1.jpg (287.9 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Mar 16, 2016 at 05:37 PM

I'm not sure but I suspect you get this error because you are trying to access and element of the Block, or BlockCounter arrays without checking them first. For example, what happens if your FindGameObjectsWithTags call, fails to find anything?
It also looks like you don't initialize the BlockCounter array at all. This would be ok, if the user entered the array properly in the editor, but that is a dangerous assumption to make. But based on the error, I suspect this array is completely uninitialized.

I also notice that your loop is going from 0 to 10, but we don't know for sure that is actually how big the arrays are!

Suggest you take a look at working with arrays in c#. In particular take a look at how to instantiate arrays (using new), and how to determine array length (gets tricky with multi-dimensional arrays like BlockCounter. Here are some links to help with that:

https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

Comment
Add comment · Show 1 · 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
avatar image ShibaIntel · Mar 17, 2016 at 01:24 AM 0
Share

The reason why i dont instantiated the Array is because every tutorial didnt seem to instantiate like the usual For-New stuff, so i kinda think that it was used so the array can change dinamically, but i will try to look more into it. still Thx for the Reference page, i will look into it to see if i can work something out of it :)

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

63 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

Related Questions

NullRefrenceException: Object refrence not set to an instance of an object. 0 Answers

Framing multiple objects from a list along y axis only? 0 Answers

NullExecption in a communication between two scripts 0 Answers

How to change value of another gameobject through script 2 Answers

RayCasting from a Empty GameObject 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