Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kht1114 · Sep 02, 2016 at 07:05 AM · objectdetectanother script

detecting whether the object has been clicked in another script

Currently, I have an object A (attached with script A), the function of script A is that when user click on the scene, object A will appear. Object A can be clicked multiple times, depends on the user.

There will be another object called object B. When object A appears on the scene(after clicking), object B will appear next to object A.

What I want to achieve:

  • know that the object A has been clicked within script A

  • tell object B to appear on the scene

  • tell object B to appear right next to object A

So I only write a little bit on script B as I don't know where to start.... but script A is completed

     public class object_B : MonoBehaviour {
     
         public Renderer rend;
     
         // Use this for initialization
         void Start () {
             
             rend = this.GetComponent<Renderer> ();
             // make the object invisible
             rend.enabled = false;
     
         }
         
         // Update is called once per frame
         void Update () {
         
         }
     }   
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 kht1114 · Sep 04, 2016 at 12:06 PM 0
Share

Thanks a lot guys! Actually, I end up with Instantiating object B within object A's script, so sth like this: GameObject right = (GameObject)Instantiate(object_B, transform.position + (transform.right), transform.rotation );

Also need to create the the object B before instantiating it!

2 Replies

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

Answer by nj4242 · Sep 02, 2016 at 10:49 AM

You don't need to add another script for object B, Just adjust the object B next to object A as you like from the Scene, also disable it from the Inspector (you will able to do it from a checkbox beside your object name in the inspector or you can simply do it within the script itself, just write : GameObject.Find ("object_B_name").active = false; in the Start ( ) function) . Add a collider to Object A.

Also add this code to script :

 void OnMouseDown()
     {
         GameObject.Find ("object_B_name").active = true;
     }

Attach the script and collider to A only, no need to add anything to B.

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
0

Answer by b1gry4n · Sep 02, 2016 at 10:03 AM

Sounds like you should have some sort of "manager" script. Have the manager spawn in object A and object B. If object A or object B are clicked, it reports the click back to the manager. From there you can do whatever. When the objects are spawned in, access the script and make a reference to the manager.

On the objects you could have something like this...

     public class AnObject : MonoBehaviour
     {
         public enum ObjectType
         {
             objectA,
             objectB
         }
         public ObjectType typeOfObject;
         public ObjectManager manager;
 
         //called by the objectmanager to set this object up
         public void Setup(ObjectManager o, ObjectType ot)
         {
             typeOfObject = ot;
             manager = o;
         }
 
         //when this object is clicked, call this
         public void Clicked()
         {
             manager.Clicked(this);
         }
     }

And your manager could be:

 public class ObjectManager : MonoBehaviour
     {
         public GameObject objectPfb;
 
 
         void Start()
         {
             SpawnObject(Vector3.zero, AnObject.ObjectType.objectA);
         }
 
         //spawns an object at the pos as the type
         void SpawnObject(Vector3 pos, AnObject.ObjectType ot)
         {
             //instantiate the prefab of our object
             GameObject g = (GameObject)Instantiate(objectPfb, pos, Quaternion.identity);
             //setup the object
             g.GetComponent<AnObject>().Setup(this, ot);
         }
 
         //called by an object when that object is clicked
         public void Clicked(AnObject objClicked)
         {
             //do whatever you want with this object
             if (objClicked.ObjectType == AnObject.ObjectType.objectA)
             {
                 SpawnObject(new Vector3(objClicked.transform.position.x + 1, objClicked.transform.position.y, objClicked.transform.position.z), AnObject.ObjectType.objectB);
             }
             else if (objClicked.ObjectType == AnObject.ObjectType.objectB)
             {
                 //whatever you want with object B
             }
         }
     }
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

detect if i'm looking at specific object 1 Answer

if object is destroyed 2 Answers

How to shoot a bullet based on another object's position? 1 Answer

Detect radius from object 1 Answer

Detect collision with specified object 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