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 laurienash · Sep 18, 2013 at 09:04 PM · gameobjecttagfind-gameobject

Replace variable with new game object after it is made null - not working

Hi - I've written a script so that a gameobject follows the first and third person controller, depending on which one is active. (This bit works). Halfway through the game- the third person controller is destroyed and a new third person controller is created.

I then want the gameobject to follow the new third person controller, once the original third person controller is destroyed - but I can't work out how to correctly phrase it in the script.

This is what I've got - it's not throwing errors, but the gameobject isn't following the new third person controller as it should:

 var targetF : Transform;
 var targetT : Transform;
 var firstPerson : GameObject;
 
 
 function Update () {
 
     if (firstPerson.GetComponent(CharacterController).active) {
     
     transform.position = Vector3.MoveTowards(transform.position,targetF.transform.position, 0.03f);
     
     transform.LookAt(targetF);
     
     }
     
     if (!firstPerson.GetComponent(CharacterController).active) {
     
     transform.position = Vector3.MoveTowards(transform.position,targetT.transform.position, 0.08f);
     
     transform.LookAt(targetT);
 
     
     }
     
     if (targetT == null) {
     
     var targetT = GameObject.FindGameObjectWithTag("Carl");
     
     }
 
 }

Any ideas on how to phrase it better?

Thanks, Laurien

Comment
Add comment · Show 7
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 DannyB · Sep 18, 2013 at 09:12 PM 1
Share

If I were you, I would avoid GetComponent and FindGameObject inside an Update loop. Then, have a GameObject that is the follow target ( GameObject followTarget ) and always follow it. Finally, set this object to point to the correct controller (i.e. when you destroy one, make it equal the other).

avatar image laurienash · Sep 18, 2013 at 09:15 PM 0
Share

Thanks! But how do I make the game object equal to the other? (Sorry - new to coding)

avatar image DannyB · Sep 18, 2013 at 09:27 PM 1
Share

Would something like this work for you? (pseudocode):

 var controller1 : GameObject;
 var controller2 : GameObject;
 var followTarget : GameObject;
 
 function Start() {
     followTarget = controller1;
 }
 
 function Update() {
     $$anonymous$$oveTowards( followTarget );
 
     if( Condition To Destroy controller1 ) {
         Destroy( controller1 );
         followTarget = controller2;
     }
 }


avatar image DannyB · Sep 18, 2013 at 09:48 PM 1
Share

Your error I am guessing is at followTarget = targetT;. Convert private var followTarget: GameObject; to private var followTarget: Transform; and it should work. The error tells you you are using GameObject where you should be using Transform.

As for the Condition to Destroy - I was only using this text to say: this is where you should write any condition that causes the destruction of one controller, and switch to the other.

avatar image DannyB · Sep 19, 2013 at 08:55 AM 1
Share

Yeah, that's probably one way to handle this. You should probably search for information about referencing other objects and components, there are many different ways of doing it, each more suitable for different situations.

Show more comments

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

16 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

Related Questions

Shooting Specific GameObject via Tag 0 Answers

Is it possible to create a tag programmatically? 5 Answers

Need to select whatever object is at a certain position 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GameObject.findGameObjectsWithTag returning empty? 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