Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 thieum · Sep 09, 2011 at 12:44 AM · c#arraywaypoints

array using getcomponents (c#)

Hello am struggling with the ol' js AIcar_script :°) Thanks to Guyt for converting it in c# in another post... BUT... I still can't get it work in C#, when the js is doing the routine... I get "Obj ref not set to an instance" pointing at this line :

 Vector3 RelativeWaypointPosition = transform.InverseTransformPoint( new Vector3(waypoints[currentWaypoint].transform.position.x, transform.position.y, waypoints[currentWaypoint].transform.position.z ) );

I tried other ways, but it seems the c# script can't retrieve the potentialWaypoints from the GameObject. :°( I hope someone can help since it's the only example of my knowledge using "getcomponentsinchildren" to populate the array, rather than instantiating the waypoints manually.

Below the condensed version of the waypoint system : Thank for your help!

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class AICar : MonoBehaviour {
     
         public GameObject waypointContainer;
         private List <Transform> waypoints;
         public int currentWaypoint = 0;
     
     
         void Awake () {
         GetWaypoints();
         }
     
     
         void GetWaypoints ()
         {
             if (waypointContainer!=null)
             {
                 Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren <Transform> ();
                 waypoints = new List<Transform>();
                 foreach (Transform potentialWaypoint in potentialWaypoints) 
                 {
                     if (potentialWaypoint != waypointContainer.transform)
                         waypoints.Add(potentialWaypoint);
                 }
             }
         }    
     
     
         void Cruise () {
             Vector3 RelativeWaypointPosition = transform.InverseTransformPoint( new Vector3(waypoints[currentWaypoint].transform.position.x, transform.position.y, waypoints[currentWaypoint].transform.position.z ) );
             Vector3 RelativeWaypointDir = ((waypoints[currentWaypoint].transform.position) - transform.position);
             float currentAngle = Vector3.Angle(RelativeWaypointDir, transform.forward);
 
 // then IA torque, steering, or animated NPC can use the previous these values
 
             
             if ( RelativeWaypointPosition.magnitude < 15 ) 
             {
                 currentWaypoint ++;
                 
                 if ( currentWaypoint >= waypoints.Count )
                     currentWaypoint = 0;
             }
         }
     }
Comment
Add comment · Show 1
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 aldonaletto · Sep 09, 2011 at 01:04 AM 0
Share

This script is new for me, but as far as I know you must create an empty object (the container) and child all waypoint objects to it, then assign the container to waypointContainer. Just to check: have you did this?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Sep 09, 2011 at 01:33 AM

Just analyse the line. Null reference exceptions happens when you try to use a class instance but the variable doesn't hold a reference to a valid object.

Vector3 RelativeWaypointPosition =                        // 1
    transform.InverseTransformPoint(new Vector3(          // 2
        waypoints[currentWaypoint].transform.position.x,  // 3
        transform.position.y,                             // 4
        waypoints[currentWaypoint].transform.position.z   // 5
    ));

  • 1: should be no problem, creating a variable that way can't cause a null ref exception.
  • 2,4: As long as your script is a Monobehaviour-component it have to be attached to a GameObject and therefore there have to be a transform component. The inverse transform and the new Vector3 also should be no problem since we can be sure the transform is there.
  • 3,5: that two "parts" look like it can cause that error. I guess the currentWaypoint variable is in bounds of the array (which means greater or equal to 0 and less than array.Length) otherwise you would get an out-of-bounds-exception. It looks like the only thing left over are the objects stored in the array.

Make sure that every element in the array is filled with a valid GameObject. If you set the array-size to 10 but you only drop 3 waypoints into the array you will have 7 null-references which would cause that error when you try to access it's transform.

Reduce the array size to the actual size or fill in the other waypoints.

Comment
Add comment · Show 4 · 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 Bunny83 · Sep 09, 2011 at 01:51 AM 0
Share

I've actually didn't read the whole script. I was thinking your waypoints variable is an array but it's a generic List.

Are you sure that you assigned your waypoint container to the variable? If not the whole waypoints List doesn't get created at all.

To spot such errors just place some temporary Debug.Logs in those functions to see what gets executed and what didn't.

Btw: You store Transforms in the List so there's no need to write

 waypoints[currentWaypoint].transform.position.x

since waypoints contains Transforms just write

 waypoints[currentWaypoint].position.x

otherwise you tell Unity to search for the first Transform component on the Gameobject of this Transform (which IS that Transform). It's like writing something like:

 transform.transform.gameObject.transform.position.x
avatar image thieum · Sep 09, 2011 at 01:55 AM 0
Share

Thank you all for your comments... Well, as said first, I run the "same" script but in js aside, with +400 waypoints in the waypointContainer... all working fine in the case of the javascript array. C# arrays are a bit more complex apparently...

I left out the code lines that drive the NPC, mostly pointing at another script (reason why I would like to have all in c#)... As a transform component, I use myTransform = transform, what I left out as well. So I pasted here only the waypoints system.

BTW I can't really get what Transform is concerned by the first "transform.InverseTransformPoint" ? The script transform or the Waypoint one? But anyway, I get the same Null ref with the following RelativeWaypointDir

:(

avatar image thieum · Sep 09, 2011 at 02:06 AM 0
Share

Yes Bunny, I just noticed the redundance as well... I fixed it but the Null ref persists. The use of the generic list is motivated by the need to have a flexible array at edit time.

avatar image thieum · Sep 09, 2011 at 08:40 AM 0
Share

Still stuck... I tried to turn the generic list into an arraylist, but do you know why the components are not accessible then? It returns "Type object' does not contain a definition for Transform' ???

avatar image
1

Answer by thieum · Sep 11, 2011 at 03:34 AM

Back in the topic, I could finally make it work :°D

  • I changed "void Start" into "void Awake", after I run into this :

"Note for C# and Boo users: use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time."

  • and I added a "waypoints.count" to be sure to keep within the array bounds

            foreach (Transform potentialWaypoint in potentialWaypoints) 
             {
                 if (potentialWaypoint.transform != waypointContainer.transform)
                     for (int i=0;i<waypoints.Count;i++);
                     waypoints.Add (potentialWaypoint);    
                     
             }
    
    

So this waypoints system is working fine!

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
avatar image
0

Answer by DaveA · Sep 09, 2011 at 12:59 AM

Make sure currentWaypoint is in bounds

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Assign a String to Each int 1 Answer

List out all Array Values. 1 Answer

How to Clear Array or Remove it's items 3 Answers


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