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 Folero · Mar 20, 2014 at 10:45 AM · c#movementwaypointwaypointscs

Object reference not set to an instance of an object

Hello,

I'm quite new to programming and I've been trying to make a waypoint system for my game so my units can move from place a to b through waypoints, here are my 2 scripts I've made.

This is my unit Script

 using UnityEngine;
 
 using System.Collections;
 
 public class Unit: MonoBehaviour
 {
         public int next = 1;
         public float speed = 10.0f;
         Transform destination;
     
         void Update ()
         {
 
                 Waypoint asd = findNextTarget ();
                 setDestination (asd.transform);
 
                 float stepSize = Time.deltaTime * speed;
         
                 transform.position = Vector3.MoveTowards (transform.position, destination.position, stepSize);
         
 
                 if (transform.position.Equals (destination.position)) {
             
                         if (next == 6) {
                                 Destroy (gameObject);
                         }
                         next++;
         
             
 
                 }
 
 
         
         }
     
         Waypoint findNextTarget ()
         {
         
                 Waypoint b = null;
 
         
                 Waypoint[] petes = (Waypoint[])FindObjectsOfType (typeof(Waypoint));
         
 
 
                 
                 for (int i = 1; i < petes.Length; ++i) {
                     
                         if (petes [i].wp == next) {
                     
                                 b = petes [i];
                         }        
                 }
                 return b;    
 
             
         }
     
         public void setDestination (Transform v)
         {
         
                 destination = v;
         
         }
     
 }


and this is my Waypoint script:

 using UnityEngine;
 
 using System.Collections;
 
 public class Waypoint : MonoBehaviour
 {
     
         public int wp = 1;
 
 }

When I'm trying to run a project I get this error: Object reference not set to an instance of an object. It refers to line 15 of my unit code.

I've attached the unit code to a cube gameobject and waypoint script to 6 different sphere objects.

I've set the next variable to 1 for the Unit and I've set wp to 1,2,3,4,5 and 6 to each 6 sphere objects.

Thanks in advance, I just can't figure out what's wrong.

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 mattyman174 · Mar 20, 2014 at 10:47 AM 0
Share

Could you copy and paste all your Errors that you see in the console please. Do not paraphrase them.

avatar image Folero · Mar 20, 2014 at 10:50 AM 0
Share

NullReferenceException: Object reference not set to an instance of an object Unit.Update () (at Assets/Unit.cs:15)

This is the error that I'm getting

1 Reply

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

Answer by mattyman174 · Mar 20, 2014 at 11:01 AM

 if (next == 6) {
 Destroy (gameObject);
 }
 next++;

Change to:

 if (next < 6) { //  Changed == to <
 Destroy (gameObject);
 }
 next++;
Comment
Add comment · Show 3 · 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 Folero · Mar 20, 2014 at 11:09 AM 0
Share

Did not fix the issue, still get the same error

avatar image mattyman174 · Mar 20, 2014 at 11:23 AM 0
Share

Yep i see now. $$anonymous$$eep that code block the same (so next == 6)

In your findNextTarget() function, you need to initialise your i variable in your for loop to equal 0, not 1.

As such.

 for (int i = 0; i < petes.Length; i++) // Notice int i = 0 (not 1)
         {
 
             if (petes[i].wp == next)
             {
 
                 b = petes[i];
             }
         }

Try that.

Also, as an Edit i just noticed, unless you intentionally tried to Pre Increment your i variable, i suggest you stick with Post Increment as i have done above. Unless you understand the consequences of such actions just stick with Post increment.

avatar image Folero · Mar 20, 2014 at 11:37 AM 0
Share

Thanks it works!

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

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

Related Questions

What is the cause of the distorted movement of my character? 0 Answers

Two and fro movement of the object at waypoints 1 Answer

How to play an animation with waypoints 1 Answer

How do i make multiple waypoints? 1 Answer

Stop player running at wall 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