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 Mister Jason · Apr 12, 2011 at 06:31 AM · gameobjectnullreferenceexceptioninstancewaypoint

Waypoint NullReferenceException: Object reference not set to an instance of an object error please help.

ok i keep getting this error but i have no idea how to fix it. Please help. :)

i basically have 3 script. One is the waypoint script, second is a player script and enemy script. Player script have a function call FindClosestEnemy to find the closest waypoint around the player. Then it store the name in to the static var closestpos:GameObject. After this i want in my WAYPOINT script var closest = playerscript.closestpos. And caculate by if/else function weather the waypoint.transform.position.z is smaller or bigger than the closestpos.transform.position. if yes do sth...if no do sth...etc.

i able to make it work in game scene, but it just keep poping up this error in my console. Can someone help me please. /bow

MY PLAYER SCRIPT

static var closestpos: GameObject;

function Update()

{

FindClosestEnemy();

}

function FindClosestEnemy () : GameObject {

// Find all game objects with tag Enemy

var waypoint : GameObject[];

waypoint = GameObject.FindGameObjectsWithTag("waypoint");

var closest : GameObject;

var distance = Mathf.Infinity;

var position = transform.position;

for (var closestwaypoint : GameObject in waypoint) {

 var diff = (closestwaypoint.transform.position - position);

 var curDistance = diff.sqrMagnitude; 

 if (curDistance < distance) { 

     closest = closestwaypoint; 

     distance = curDistance; 

     closestpos = closest;


 } 

} return closest;

}

MY WAYPOINT SCRIPT

var waypoint : Transform[];

private var currentWaypoint: int;

var player : Transform;

var saveTime = 0;

private var closest: GameObject;

function Update()

{

Debug.Log(playerscript.closestpos.transform.position);

closest = playerscript.closestpos;

var position = transform.position;

if(transform.position.z < closest.transform.position.z)

//NullReferenceException: Object reference not set to an instance of an object waypoint.Update () (at Assets/scripts/waypoint.js:15)

{ currentWaypoint=1;

} else {

if(transform.position.z > closest.transform.position.z)

{ currentWaypoint=0;

} else {

if(transform.position.x > closest.transform.position.x)

{

currentWaypoint=2;

} else {

if(transform.position.x < closest.transform.position.x)

{

currentWaypoint=3; } } } } }

function OnTriggerEnter (enemy : Collider) {

if (enemy.gameObject.tag == "enemy")

{

var rotation = waypoint[currentWaypoint].rotation;

var position = waypoint[currentWaypoint].position;

enemy.transform.LookAt(position);

}

}

Comment
Add comment · Show 4
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 Mister Jason · Apr 12, 2011 at 08:36 AM 0
Share

..humm to make question simpler.. why this line have error?

" if(transform.position.z < closest.transform.position.z) "

the var called closest = playerscript.closest and the playerscript.closest is a static var : GameObject

avatar image Owen-Reynolds · Apr 12, 2011 at 03:50 PM 0
Share

Double-click the error to find the line. Exactly what is it looking for that is NULL? $$anonymous$$aybe you don't any objects tagged as "waypoint"? $$anonymous$$aybe it can't find playerScript? ... . When you get a error like this, it skips the rest of the script, but runs again next frame.

avatar image Mister Jason · Apr 13, 2011 at 12:49 AM 0
Share

when i double kick it. the error happen in this line

if(transform.position.z < closest.transform.position.z)

avatar image Mister Jason · Apr 13, 2011 at 01:19 AM 0
Share

ooh i fix it... i need make a yield waitforseconds to delay the if/else function

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

Spawning random objects. Trouble /w objects. Null Reference. 0 Answers

How to use if statement properly 1 Answer

How can I reference in a prefab's script a gameObject found in the scene 1 Answer

Dynamically Changing GameObject makes NullReferenceException(C# Unity) 1 Answer

NullReferenceException in follow function 0 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