• 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 /
  • Help Room /
avatar image
0
Question by Grish · Apr 19, 2016 at 11:49 PM · transforminstantiation

How to pass position information between two objects?

Hi,

I have a randomly spawning set of cubes using the instantiate command using C sharp.

Once you click on the cube a sphere is spawned, again in a random place.

This all works fine (lots of spheres are spawned when you keep clicking on different cubes). What I need now is for the sphere to go towards the very cube that I clicked for it to spawn. Not just any cube.

I'm relatively new to this so I thought of two ways this might work: 1) Storing the coordinates of the cube in a temporary object. 2) Adding the spheres as children to the cubes

What I'm essentially looking for is a for the cube to create the sphere and then say "Oh, btw here are my coordinates, come get me!"

Please let me know if there is a simple way of doing this (I just know there is!).

Thank you for the help.

Regards, G

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 Thijsx7 · Apr 20, 2016 at 08:22 PM 1
Share

Hey

Before I can fully answer your question I need to know one more thing: When you spawn a set of cubes, do they all have different names in the editor like: cube1, cube2, cube3. Or are they all named cube? Na$$anonymous$$g the cubes will make everything easier because then you can just make cube1 spawn sphere1 and give sphere1 a script to move to cube1. Sending the scripts you use would really help me too as I would be able to fully understand what you mean.

Greetings, Thijs

Show more comments

2 Replies

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

Answer by Jessespike · Apr 20, 2016 at 08:41 PM

Make a script that follows a position or transform. Then when you Instantiate the object (sphere), use GetComponent to access the follow script and set the target. For example:

 public class MoveTowardsTarget : MonoBehaviour {
 
     public Transform target;
     public float moveSpeed = 0.1f;
 
     // Update is called once per frame
     void Update () {
         if (target != null)
         {
             this.transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed);
         }
     }
 }

 public class SpawnOnClick : MonoBehaviour {
 
     public GameObject spawnObject;
 
     void OnMouseDown()
     {
         Vector3 randomPos = new Vector3(Random.Range(-10, 10), Random.Range(-10, 10), 0);
         GameObject newObject = Instantiate(spawnObject, randomPos, Quaternion.identity) as GameObject;
         MoveTowardsTarget moveScript = newObject.GetComponent<MoveTowardsTarget>();
         if (moveScript != null)
         {
             moveScript.target = this.transform;
         }
     }
 }
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 Grish · Apr 22, 2016 at 11:03 PM 0
Share

Thank you, I'll give this a go and see if it works for me.

avatar image Grish · Apr 25, 2016 at 06:50 PM 0
Share

Hey,

So this seems to work,. except that the spheres are moving to the previous cube, rather than the one I need.

 using UnityEngine;
 using System.Collections;
 
 public class attackEnemy : $$anonymous$$onoBehaviour {
     public GameObject hero;
     public int maxHeroes = 3;
 
     public void On$$anonymous$$ouseDown (){
         if (maxHeroes > 0) {
             Debug.Log ("click");
             Instantiate (hero, new Vector3 (Random.Range (10, -10), 0, Random.Range (10, -10)), Quaternion.identity);
             maxHeroes--;
 
             Debug.Log (hero.GetComponent<$$anonymous$$oveTowardsTarget> ());
             $$anonymous$$oveTowardsTarget moveScript = hero.GetComponent<$$anonymous$$oveTowardsTarget>();
             if (moveScript != null)
             {
                 //Debug.Log ("$$anonymous$$oving coordinates found");
                 moveScript.target = this.transform;
                 Debug.Log (moveScript.target);
             }
         }
     }
 }
 

Would you know why this is?

avatar image
0

Answer by Nameless1995 · Apr 20, 2016 at 08:33 PM

Just declare a vector3 type variable in which you can store the position information of the cube... then you can just use the movetowards function or something to move the sphere towards the Vector3 position,

Comment
Add comment · Show 1 · 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 Grish · Apr 22, 2016 at 11:02 PM 0
Share

Thanks for your suggestion but I don't fully follow. How would the sphere know which cube to move towards?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Gameobjects instantiating in the wrong position 0 Answers

Cannot Parent a Prefab Instantiated with Resource.Load() 1 Answer

How to set the parent of an instantiated game object 1 Answer

Firing projectile from Transform. What am I doing wrong? 1 Answer

Linecast with transforms rotation? 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