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 ReContraChanfle · Feb 18, 2014 at 06:15 PM · gameobjectinstantiatecloned

Get transform of an instantiated GameObject(clone)

Hi, i have a problem with get the transform of an instantiated Gameobject(clone), i wanna get the trasform to use it in other script, i only achieve a null reference.

 public class Enemy : MonoBehaviour {
 
     
     public float activeZone;
     float nextFire;
     public float Distance;
     public Transform player;
     public Transform FirePosition;
      Transform enemy;
     GameObject Bullet;
      Vector2 posPlayer;
      Vector2 posBullet;
 
     
     
     void Start () 
     {
         nextFire = 0f;
         activeZone = 12f;
         player = GameObject.FindGameObjectWithTag ("Player").transform;
         FirePosition = GameObject.FindGameObjectWithTag ("Fire").transform;
         Bullet = GameObject.FindGameObjectWithTag("Bullets");
 
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         if (Distance < activeZone) {
             
             if (Input.GetKeyDown (KeyCode.F) && Time.time > nextFire + 2.0f) {
 
                 nextFire = Time.time;
                 GameObject BulletInstance;
                 BulletInstance = Instantiate (Bullet,FirePosition.position, FirePosition.rotation) as GameObject;
                 BulletInstance.rigidbody2D.AddForce (new Vector2 (-5000f, -30f));
                 enemy = BulletInstance.transform;
                 Destroy(BulletInstance,0.5f);
             }    
         }
                 
         }
         
 
     void FixedUpdate()
     {
 
         posPlayer = player.position;
         posBullet = FirePosition.position;
         Distance = Vector2.Distance (posPlayer, posBullet);
     }


Comment
Add comment · Show 2
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 whydoidoit · Feb 18, 2014 at 06:19 PM 0
Share

It looks like enemy should be right - for 0.5 seconds anyway, after that it will be null.

avatar image ReContraChanfle · Feb 19, 2014 at 01:15 AM 0
Share

yes, in that 0.5 seconds i wanna get the distance between the proyectile and the player, for the score, for now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by F-N · Feb 18, 2014 at 06:31 PM

Try finding it directly after you instantiate it:

enemy = GameObject.Find("Bullet(Clone)").transform;

Comment
Add comment · Show 4 · 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 whydoidoit · Feb 18, 2014 at 06:42 PM 0
Share

The OP has the object that they have already instantiated.

avatar image ReContraChanfle · Feb 19, 2014 at 01:12 AM 0
Share

Thanks, but already got the problem, i added this two lines after instantiate, and problem solved,

 if(BulletInstance!=null)
                 enemy = GameObject.Find ("Bullets(Clone)").transform;
 

an evry time i want to use this clone(transform) first i have check if its not null.

avatar image whydoidoit · Feb 19, 2014 at 02:30 AM 0
Share

There is no way you should be finding it like this. You are instantiating it. This code would never work properly if there were two or more bullets at once.

avatar image ReContraChanfle · Feb 19, 2014 at 05:35 AM 0
Share

in the debug.log i get the actual position of the clone, maybe its not getting all the positions of all bullets, tomorrow will check it,

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

20 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 avatar image avatar image avatar image avatar image

Related Questions

Combine objects instantiated at runtime 2 Answers

Instantiate as child 3 Answers

GameManager class doesn't accept GameObject.Find() 0 Answers

Split a cube into several pieces? 1 Answer

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 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