- Home /
How to use a list to populate a target object?
The intended behavior is that the GroundRoom object should pass a list of objects of interest to the director script. The director script then instructs the controller script to move the gameobject to the target (in this case the first object in the list).
However, I keep getting null reference exceptions. Is this because the object is never properly added to the list? How do I fix this?
Thank you in advance for your help. (Edit: updated code and included runtime errors below.)
ObjectOfInterest.cs
using UnityEngine;
using System.Collections.Generic;
public class ObjectOfInterest : MonoBehaviour {
public string objectName;
private Room room;
// Use this for initialization
void Start () {
room = gameObject.GetComponent<Room>();
room.objectsInRoom.Add(gameObject);
}
// Update is called once per frame
void Update () {
}
}
Room.cs
using UnityEngine;
using System.Collections.Generic;
public class Room : MonoBehaviour {
public List<GameObject> objectsInRoom = new List<GameObject>();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Director.cs
using UnityEngine;
using System.Collections;
public class Director : MonoBehaviour {
private GameObject target;
private Controller controller;
private GameObject roomObject;
private Room roomScript;
// Use this for initialization
void Start () {
controller = gameObject.GetComponent<Controller>();
roomObject = GameObject.Find ("GroundRoom");
roomScript = roomObject.GetComponent<Room>();
}
// Update is called once per frame
void Update () {
target = roomScript.objectsInRoom[0];
}
public GameObject Target () {
return target;
}
}
Controller.cs
using System.Collections;
public class Controller : MonoBehaviour {
private Director director;
public float speed;
public float rotationSpeed;
Vector3 direction = new Vector3();
Vector3 targetLocation = new Vector3();
Quaternion looking = new Quaternion();
public enum state {moving, idle};
public static state MyState;
private GameObject target;
// Use this for initialization
void Start () {
director = gameObject.GetComponent<Director>();
//set default speed and rotationSpeed;
if (speed == 0) {
speed = 100;
}
if (rotationSpeed == 0) {
rotationSpeed = 100;
}
}
// Update is called once per frame
void Update () {
target = director.Target();
MoveTo ();
RotateTo ();
}
void MoveTo () {
targetLocation = target.transform.position;
targetLocation.y = transform.position.y;
transform.position = Vector3.MoveTowards(transform.position, targetLocation, speed * Time.deltaTime);
}
void RotateTo () {
targetLocation = target.transform.position;
direction = (targetLocation - transform.position).normalized;
looking = Quaternion.LookRotation(direction);
looking.x = 0;
looking.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, looking, Time.deltaTime * rotationSpeed);
}
}
The Exceptions at runtime are:
Not getting ArgumentOutOfRangeException ever since I initialized the list.
NullReferenceException at targetLocation = target.transform.position; in the Controller script, MoveTo() function, line 36.
Could you please also mention the lines where you get those Exceptions? That would make it a lot easier for us to help you.
[edit] Also, from a quick look at your code... the "objectsInRoom" variable doesn't seem to be initialized.
Try adding the following line to the Start method of the Room class.
objectsInRoom = new List<GameObject>();
Thanks for the response. I added the initialization, still getting the errors.
The Exceptions at runtime are: ArgumentOutOfRangeException at target = roomScript.objectsInRoom[0]; in the Director script, update function.
NullReferenceException at targetLocation = target.transform.position; in the Controller script, $$anonymous$$oveTo function.
Answer by robertbu · Oct 03, 2013 at 06:44 AM
The null reference exception is because you never initialize your list. Line 26 should be:
public List<GameObject> objectsInRoom = new List<GameObject>();
Also depending on how you are using this list, you may want it to be a list of ObjectOfInterest rather than GameObject.
Thanks! I didn't realize that lists need to be treated like constructs. Unfortunately, that didn't resolve the Null Reference errors.
Please edit your question to update your code to the latest, then you need to include the error in your question. Specifically we need to know what line is generating the error.
Answer by audibles · Oct 03, 2013 at 11:12 PM
Just solved it by talking myself through the scripts. My list objectsInRoom was being initialized after ObjectOfInterest.cs tried to add a gameobject to it. Order of operations error.
Thanks for your help folks!
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How do I save a gameObject to a list so i can destroy in the scene? 1 Answer
Prefab Script List not being assigned as unique - can you see why? 1 Answer
Dynamically assign each ui.Text.text in list to the name of objects in another list 2 Answers
Trouble with Destroying and instantiating game objects on new scene 1 Answer