as the title suggests i get a null reference exception after i try to go something to an array of game objects found with “FindGameObjectsWithTag” i know its silly but ive spent half a day trying to get it to work using 2 ways.
this is 10% of actual code but it should be enough to demonstrate the issue.
public class Somescript: MonoBehaviour {
private Vector3 [] Blockpositions ;
private GameObject[] Blocks;
void Start () {
Blocks = GameObject.FindGameObjectsWithTag("Blocks");
for (int i = 0; i < Blocks.Length ; i++) {
Blockpositions _= Blocks*.transform.position;*_
and thats where i get the null reference exception on that last line. my original design which was more convenient but also threw an error if you have an idea was to call “foreach” rather than iterate like this. public class Somescript: MonoBehaviour {
int i = 0 foreach (GameObject Bl in Blocks) { Blockpositions[i++] = Bl.transform.position;
Mind you im well aware that Blockpositions = Bl.transform.position; and Bl_.transform.position = Blockpositions are two differnt things that always get me confused and thats my problem but point is i tried both ways for both methods and something is still wrong and been researching for a while and cant find where i went wrong. Thanks for your help guys. Cheers,_
Arrays in C# aren’t dynamic. You need to instantiate them before you can access them, or set their values, and you need to specify the length in order to do that. Your Blocks array is defined by the FindGameObjectsWithTag function, but your Blockpositions array isn’t, and is still null when you try to assign to it.
Oh, and as a side note, it’s generally good practice to name your variables with a lowercase first letter, and uppercase on the first letter of every new word. For types and classes, you should use uppercase for every word, so instead of this line:
public class Somescript: MonoBehaviour {
Try this:
public class SomeScript: MonoBehaviour {
And instead of this:
private Vector3 [] Blockpositions ;
Try this:
private Vector3 [] blockPositions ;
It helps to make it clear what you’re referring to, and just keeps things at little more organised.
I tried running this exact script in a simple working scene and it works just fine:
using UnityEngine;
using System.Collections;
public class SomeScript : MonoBehaviour {
private Vector3 [] Blockpositions ;
private GameObject[] Blocks;
private int blockslength;
void Start()
{
Blocks = GameObject.FindGameObjectsWithTag("Blocks");
int blockslength = Blocks.Length;
Vector3[] Blockpositions = new Vector3[blockslength];
for (int i = 0; i < Blocks.Length; i++)
{
Blockpositions _= Blocks*.transform.position;*_
Debug.Log("Easy check: " + i); } } } Also having one of the block gameobjects disabled in the scene doesn’t cause any problems, it just gets skipped. The issue likely has to do with another part of your code, or the scene itself. Try running my script in a new scene, and add: - 1 Empty GameObject with the Script on it - 4 Empty GameObjects tagged with “Blocks” Run the scene and see if it works If that doesn’t work, it may be an internal issue. I have Unity Pro version 4.5.0.f6, if that helps. If that doesn’t cause any errors, I suggest adding more of your class’ code until issues occur, if your script works, you should then have a look at the scene you’re in, does it for example change an object’s tag in some Start function that interferes with your code after it has been gathered into the array of correctly tagged objects. Or more likely, does one of the found blocks get Destroyed after it has entered the array, you should look into all those things :3 Best of luck, if you’d like more help, please post the rest of your script and perhaps screenshots of your screen, so we can have a look there as well for you Best of luck!! If you’re satisfied with this answer, please accept it, if not, I’ll be awaiting more details / questions to answer :3 Yours truly, ~ThePersister