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 thenachotech1113 · Oct 27, 2012 at 02:25 AM · transformplayerfindturret

I want to find a transform when an object is instantiated

hey, I`m trying to make a turret spawn when there are no more turrets left is the scene, but when it spawns the Player`s transform variable is empty and i cant find the way to make it find so here is what i was trying:

     var LookAtTarget:Transform;
     
     function Start () {
     
     LookAtTarget = GameObject.Find("Player");
     
     }
 
 function Update () 
 {
     distance = (LookAtTarget.position - myTransform.position).magnitude;
 
 
     if(LookAtTarget)
     {
     var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
 
     transform.rotation = Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime * damp);
 
     var seconds : int = Time.time;
 
     var evenodd = (seconds / 2);
 
     if (evenodd && distance < maxDistance) 
     {
     Shoot(seconds);
     }
     }

(i just copied what i thought was necessary because the code is too long) If some one has an idea of how to make him automatically find its target i would much appreciate it.

thanks to all

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

2 Replies

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

Answer by Epixam · Oct 27, 2012 at 05:18 PM

Perhaps part of your problem is when you do

   var LookAtTarget:Transform;
 
     function Start () {
 
     LookAtTarget = GameObject.Find("Player");
 
     }

you are trying to set the "transform" variable to a "gameobject. That cant be helping things.

I think you need to do this

         LookAtTarget = GameObject.Find("Player").transform;

Also REMEMBER that since you are instantiating the player you will need something in the update to see if the variable is NULL and if it is then find the player

 function Update()
 {
 If(LookAtTarget == null)
          LookAtTarget = GameObject.Find("Player");
 else
 {
       // do update stuff
 }
 }



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

Answer by Montraydavis · Oct 27, 2012 at 03:39 AM

I tested this, and it works.

 var prefabb : GameObject;
     
 prefabb = Instantiate (gameObject, Vector3.zero, Quaternion.identity);
 var blah : Transform = prefabb.gameObject.transform ;


PS: Just replace 'gameObject' with the instantiated object.

Comment
Add comment · Show 2 · 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 thenachotech1113 · Oct 27, 2012 at 01:32 PM 0
Share

thanks a lot, still it does not work for me, or i don`t know how to use it. so, is this supposed to make the already deployed turret to find the player?

avatar image Montraydavis · Oct 27, 2012 at 01:43 PM 0
Share

Welcome. :) Feel free to hit thumbs up if it worked.

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

11 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

Related Questions

Can't get transform of parent! 1 Answer

Why does this wont work? transfor.find() 1 Answer

Need enemy to follow only active character 0 Answers

Making a powerup that transforms player scale 1 Answer

Main Camera GetComponent doesn't work for a script? 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