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 slayer29179 · Dec 08, 2011 at 11:44 PM · errorgameobjecttransform

Converting GameObject into Transform?

Hey everyone! i have a small error and i am hoping the title is right for my problem, but sorry if it isn't.

I have a game in which the user can click on the floor and a node will appear on the floor where the mouse is, and if they click anywhere else the node will move to that location. This code works perfectly.

I also have another code in which uses the Nodes as way points in which my tank will follow when its clicked. I have the click code working, but i am trying to get the way point code to search the Game world for the Node in which i Instantiated with the click code. I can make it find it using Game Objects but the code requires it to be a transform variable. Please help me my way point code is,

 var waypoint : Transform;
 var speed : float = 20;    
 static var pointput = false; 
 
 function Update ()    
 {    
     if (Waypoints.pointput == true)    
     {    
         waypoint = transform.Find("Tank_Waypoint(Clone)");//i am guessing this is the problem?    
         if (Unit_Press.selectionhudon == true)    
         {    
         var target : Vector3 = waypoint.position;//This is the line with the error  
         var moveDirection : Vector3 = target - transform.position;    
         var velocity = rigidbody.velocity;    
         if(moveDirection.magnitude < 1)    
         {    
                     velocity = Vector3.zero;    
                 }    
                 else    
                 {    
                     velocity = moveDirection.normalized * speed;    
                 }    
             }    
             rigidbody.velocity = velocity;    
             transform.LookAt(target);    
     }    
 }

When run i click on my tank, and it puts the selection box around it, then when i right click on the floor it places the Node but shows an error when the tank try's to follow it. The waypoint code does work when the nodes are already in the scene but i am trying to make it so i have to place the nodes. The error is,

NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19) Waypoints.Update () (at Assets/Scripts/Waypoints.js:12)

please help me! thank you very much to who ever does

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 thevraptor · Jul 26, 2014 at 04:08 AM 0
Share

This sounds like a pretty kickass RTS game!

1 Reply

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

Answer by dannyskim · Dec 08, 2011 at 11:55 PM

Most likely the error is actually coming from your transform.Find:

 waypoint = transform.Find("Tank_Waypoint(Clone)");

No exception is thrown if it returns null, that's why it errors out at the line that you commented after where the debugger is reporting the issue.

I'm not quite sure if this is true, but the Unity documentation for transform.Find states that it looks for objects that are children of the object that you are calling the function from:

http://unity3d.com/support/documentation/ScriptReference/Transform.Find.html

You may want to try changing that line of code to:

 waypoint = GameObject.Find( "Tank_Waypoint(Clone)" );

and changing the corresponding line of code to:

 var target : Vector3 = waypoint.transform.position;

and changing your variable declaration at the top to:

 var waypoint : GameObject;
Comment
Add comment · Show 5 · 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 slayer29179 · Dec 09, 2011 at 12:01 AM 0
Share

ahah! thank you my friend! it works :D i wish i could rate twice for the speed of your reply!

avatar image dannyskim · Dec 09, 2011 at 12:02 AM 0
Share

anytime bud! glad that fixed it

avatar image MegaUltimateHD · Jul 05, 2016 at 10:21 AM 0
Share

How would you make it ins$$anonymous$$d of finding the object by name but by tag?

waypoint = GameObject.Find( "Tank_Waypoint(Clone)" );

avatar image Fire_Cube MegaUltimateHD · Jul 05, 2016 at 12:10 PM 0
Share

GameObject.FindGameobjectWithTag("some tag");

take a look in the autocompletion possibilities before asking :)

avatar image MegaUltimateHD Fire_Cube · Jul 06, 2016 at 09:59 AM 0
Share

Thanks :) I will take your tip on board. Sorry for any inconvenience

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

7 People are following this question.

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

Related Questions

transform.parent can't go with gameObject.find 3 Answers

I am getting an error and cant figure out why, Help please? 1 Answer

Need workaround for this line of code 1 Answer

Weird compilation error regarding Instantiate 0 Answers

NaN exception when spawning soldiers 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