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 Zacharianinjaa · Jul 20, 2012 at 04:40 AM · javascriptmovenullwaypoint

Null Reference with transform

Im trying to make my var Player move to the position of the object Waypoint. Player is an empty in my game and WayPoint and PlayerObj are both children of it. I thought i had it but whenever i run it i get a null reference. any help would be great

here is my code:

var Moving : boolean = false;

var speed : int = 5;

function Update(){

var waypoint = transform.Find("WayPoint"); var player = transform.Find("PlayerObj");

 if (Moving == true){
   player.position = Vector3.MoveTowards(player.position, waypoint.position, speed*Time.deltaTime);
 } 

}

function ActiveMove () {

Moving = true; }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Jul 20, 2012 at 05:20 AM

for starters, get that line out of the update and into the start function... make sure you declare the vars outside the function.

otherwise, you should print the null reference for clarity.. maybe the name is not exactly correct? or maybe you are trying to access the vars player or waypoint from another script? can we see the null reference please

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 Zacharianinjaa · Jul 20, 2012 at 02:48 PM 0
Share

I'm not exactly sure how to print a null reference sorry I'm still kind of a noob. But heres the line from the debugger, NullReferenceException

UnityEngine.Transform.get_position () (at C:/BuildAgent/work/b0bcff80449a48aa/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) Player$$anonymous$$ove.Update () (at Assets/Scripts/Player/Player$$anonymous$$ove.js:20)

I also moved the variables outside like you suggested

avatar image Seth-Bergman · Jul 20, 2012 at 10:34 PM 0
Share

looks like the null reference is co$$anonymous$$g from the script "Player$$anonymous$$ove", on line 20. What is on line 20?

also, I suspect the prob is "var player = transform.Find("PlayerObj");"

understand that Transform.Find uses the exact hierarchy, so only if PlayerObj is directly childed to Player will this work..

avatar image Zacharianinjaa · Jul 21, 2012 at 12:43 AM 0
Share

Line 20 reads :

Player.position = Vector3.$$anonymous$$oveTowards(Player.position, Waypoint.position, speed*Time.deltaTime);

and both PlayerObj and Waypoint are two separate children of the empty named Player so i believe that makes them both direct children

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

move childA to position of childB 1 Answer

Move Object Left/ Right 0 Answers

Waypoint System help 1 Answer

Best Way to make a character move 1 Answer

OnMouseDown carry out whole function problem 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