- Home /
Object reference not set to an instance of an object in functions
I have a function in other script, but when I call it from other script and give a GameObject as parameter it gives error.
public void Generate (Vector3 hit){
this.transform.position = hit;
Vector3 position = this.transform.position;
position.x = Mathf.Round(position.x / snap) * snap;
position.y = Mathf.Round(position.y / snap) * snap;
position.z = Mathf.Round(position.z / snap) * snap;
this.transform.position = position;
Vector3 gridStart = transform.position - new Vector3(worldX, worldY, worldZ);
for(float x =0F; x<worldX; x += snap) {
for(float y =0F; y<worldY; y += snap) {
for(float z =0F; z<worldZ; z += snap) {
GameObject gridCube = Instantiate(prefab) as GameObject;
Vector3 startPosition = transform.position + new Vector3(worldX, worldY, worldZ);
gridCube.transform.position = new Vector3(gridStart.x + x, gridStart.y, gridStart.z + z);
gridCubesList.Add(gridCube);
}
}
}
IsGenerated = true;
}
public Vector3 snapPoint (GameObject GroundObject){
Debug.Log(GroundObject);
List<GameObject> freeCubesList = new List<GameObject>();
GameObject[] freeCubes = null;
List<GameObject> GroundObjectCubesList = new List<GameObject>();
GameObject[] GroundObjectCubes;
List<GameObject> possibleCubesList = new List<GameObject>();
GameObject[] possibleCubes = null;
List<Vector3> localPositionsList = new List<Vector3>();
Vector3[] localPositions = null;
List<Vector3> globalPositionsList = new List<Vector3>();
Vector3[] globalPositions;
List<float> distanceList = new List<float>();
float[] distances = null;
int minIndex; // Shortest distance
GroundObject.transform.position = this.transform.position;
foreach (GameObject gridCube in gridCubesList){
gridTrigger trigger = (gridTrigger) gridCube.GetComponent(typeof(gridTrigger));
if(trigger.isTriggered(GroundObject)){
localPositionsList.Add(this.transform.position - gridCube.transform.position);
localPositions = localPositionsList.ToArray();
GroundObjectCubesList.Add(gridCube);
GroundObjectCubes = GroundObjectCubesList.ToArray();
}
if(trigger.isTriggered(null) != true){
freeCubesList.Add(gridCube);
freeCubes = freeCubesList.ToArray();
}
}
foreach (GameObject current in freeCubes){
foreach(Vector3 localPosition in localPositions){
foreach (GameObject freeCube in freeCubes){
Vector3 globalPosition = freeCube.transform.position + localPosition;
globalPositionsList.Add (globalPosition);
globalPositions = globalPositionsList.ToArray();
if(globalPosition == freeCube.transform.position){
possibleCubesList.Add(freeCube);
possibleCubes = possibleCubesList.ToArray();
}
}
}
}
foreach (GameObject possibleCube in possibleCubes){
distanceList.Add(Vector3.Distance(possibleCube.transform.position, transform.position));
distances = distanceList.ToArray();
}
minIndex = Array.IndexOf(distances, distances.Min());
return possibleCubes[minIndex].transform.position;
}
And in other script:
GroundObject.rigidbody.MovePosition(grid.snapPoint(GroundObject) + speed * Time.deltaTime);
EDIT: So how do I share a GameObject with an istance with other script?
@Avash, please add your code, it's temping to close this since this question is probably one of the most asked, least searched for on UA.
@Avash, how is the grid object in the below being instantiated, this is actually the most important part and the part where you left out the detail. Is this a variable that is assigned in the inspector? Are you using GetComponent() to try and get a reference.
GroundObject.rigidbody.$$anonymous$$ovePosition(grid.snapPoint(GroundObject) + speed * Time.deltaTime);
Have you tried adding debugging code to ensure your objects have reference.
if(GroundObject)
Debug.Log("GroundObject isn't null");
else
Debug.Log("GroundObject is null, i should fix this");
if(grid)
Debug.Log("grid isn't null, niiiice!");
else
Debug.Log("grid is null, i should fix this because it creates exceptions");
Also, why wouldn't you give actual error that points to the line of code that the exception is thrown from?
Edit: There a lot of assumptions of existing structure in the snapPoint method, this is why the location of the error is paramount to helping you.
@Avash, (sigh) obviously there isn't a line 70 in your code that is in your original post, please indicate which line is line 70.
Answer by NoseKills · Oct 25, 2014 at 08:09 PM
You say line 19 throws the error and that's:
if(trigger.isTriggered(GroundObject)){
The only thing there that can throw a null ref error is if trigger
is null
Either your gridCube
in that loop didn't have that component or... I'm not sure if that line where you try to get the component is right
gridTrigger trigger = (gridTrigger) gridCube.GetComponent(typeof(gridTrigger));
What if you try
gridTrigger trigger = gridCube.GetComponent<gridTrigger>();
That means then that at least one gridCube
in gridCubesList
does not have a component gridTrigger
attached to it.
You'll just have to find out why that is or post all the code where gridCubesList
is populated etc...
Also you could add some Debug.Log into the loop to see if any of the gridCube
have that component or is it missing from all of them.
foreach (GameObject gridCube in gridCubesList)
{
gridTrigger trigger = (gridTrigger) gridCube.GetComponent(typeof(gridTrigger));
if (trigger != null) Debug.Log("Trigger found");
...
How many times do you get "Trigger Found" and how many entries are in gridCubesList
?
If you double click the error in Unity console, which line does it take you to in $$anonymous$$onoDevelop?
If line 19 truly was if(trigger.isTriggered(GroundObject)){
(in the original code sample) and you said that's where the error comes from, there's no other choice than trigger
being null because a function (`isTriggered`) can't be null and passing a null parameter (`GroundObject`) is totally O$$anonymous$$.