Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by audibles · Oct 03, 2013 at 05:14 AM · gameobjectlists

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadoX · Oct 03, 2013 at 06:27 AM 0
Share

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>();
avatar image audibles · Oct 03, 2013 at 05:36 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image audibles · Oct 03, 2013 at 05:29 PM 0
Share

Thanks! I didn't realize that lists need to be treated like constructs. Unfortunately, that didn't resolve the Null Reference errors.

avatar image robertbu · Oct 03, 2013 at 05:34 PM 0
Share

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.

avatar image
1

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges