Null Reference exception with custom class object
So I made a class called 'Dog' and made a constructor with it. the constructor contains some floats a int and a GameObject. I can create these 'Dog' elements at runtime, and the are stored in a list. but when I try to access them I get an error stating 'NullReferenceException: Object reference not set to an instance of an object' When I go to the inspector it shows the list and every time I hit 'd' it adds a element to the list. and is displayed in the inspector as Element 0 [Dog (none) ]. Which makes sense that it is empty and that this is causing the error. But because I am making the elements at run time I cant drag and drop a object into each element. How can I add a object to each element through the script so it can still function at runtime? Would what I just said solve my problem? Thanks!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Game;
public class SpecialFunctions : $$anonymous$$onoBehaviour {
gotoFunction mGoToFunc;
GameObject S1,S2,C1,C2;
public List<Dog> dogs;
public List<Human> humans;
float X, Y, Z;
int mId;
public int getId() {mId++; return mId;}
public GameObject randShape (string type) { // Generates the random 3-d object
switch (type){
case "Human":
GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
cube.transform.position = new Vector3 (randX (), randY (), randZ ());
cube.transform.localScale = new Vector3 (1.5f, randHeight ("Human"), 0.75f);
return cube;
break;
case "Dog":
GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
sphere.transform.position = new Vector3 (randX(), randY(), randZ());
sphere.transform.localScale = new Vector3 (1.5f, randHeight ("Dog"), 0.75f);
return sphere;
break;
default:
print ("Not valid type.");
return null;
}
}
public void addLife() { // generates the enitre object
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.H))
humans.Add (new Human (randAttack ("Human"), randHeight ("Human"), randRun ("Human"), randHealth ("Human"), randShape ("Human"), getId ()));
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D))
dogs.Add (new Dog (randAttack ("Dog"), randHeight ("Dog"), randRun ("Dog"), randHealth ("Dog"), randShape ("Dog"), getId ()));
}
public void checkForGoTo() { //checks if any objects are near eachother. if they are they goto() home base
if (dogs.Count > 2) {
for (int k = 0; k < dogs.Count - 1; k++) {
for (int i = k + 1; i < dogs.Count; i++) {
S1 = dogs [k].getShape ();
S2 = dogs [i].getShape ();
print (dogs [i].getAttack ());
if ((S1.transform.position.z - S2.transform.position.z < 2) &&
(dogs [k].transform.position.x - dogs [i].transform.position.x < 2)) {
mGoToFunc.goToStatic(dogs [k], dogs [i]);
}
}
}
}
}
}
Your code to me looks a little confusing, but to answer your question at first glance, when you create your actual Dog "prefab" or gameobject, you want to put that to a GameObject variable and add that variable to the list. $$anonymous$$ind of like this:
Dog doggy = new Dog (randAttack ("Dog"), randHeight ("Dog"), randRun ("Dog"), randHealth ("Dog"), randShape ("Dog"), getId ());
dogs.Add(doggy);
Here is my script im using a custom namespace to that is where Human and Dog come from. I cut out some of the functions because I past the character limit so the randAttack and stuff in the addLife() function are the ones that I deleted the problem is co$$anonymous$$g from the very last if statement it can not give me a current position because inside of the list is a empty Dog element. I tried accessing just the actual GameObject of the Dog object but I didnt accept that either
When I print(); Dog.getShape(); or just Dog it outputs null. So I know for sure that it is null I just do not know how to make it not be null. especially because in the list the element is Type 'Dog' the class I created and is stroed in the list of type Dog. this line below adds to the list a Dog object with all of its attributes.: dogs.Add (new Dog (randAttack ("Dog"), randHeight ("Dog"), randRun ("Dog"), randHealth ("Dog"), randShape ("Dog"), getId ()));