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.
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 :)
Sorry about that , please Look at Positive7 Reply, i really had trouble explaining the whole idea, i hope that clear the confusion.
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);
}
}
}
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):
Im sorry for any unprofessional Question or explaining, i hope this clears out the confusion, just started this.
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
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
![](https://koobas.hobune.stream/wayback/20220612072100im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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