• 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 Astha_Mishra · Jan 31, 2018 at 03:29 PM · unity 5gameobjectgameobjects

how to assign different Gameobject to a single variable during run time using c#?

Actually what i want is , on clicking over Gameobject it should be stored in a variable for accessing , but if i click some other Gameobject then i should again stored in the same object(i m doing this because i have 24 Gameobjects in a single scene and all of them need to perform action on mouse click).

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

1 Reply

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

Answer by Hellium · Jan 31, 2018 at 04:05 PM

Supposing you are working with 3D objects

  1. Add an EventSystem in your scene (GameObject > UI > EventSystem)

  2. Attach a Physics Raycaster to the event system

  3. Create an empty with the following ClickTargetsManager script:


 using UnityEngine;
 
 public class ClickTargetsManager : MonoBehaviour
 {
     public GameObject CurrentTarget;
 
     private void Start()
     {
         ClickTarget[] targets = FindObjectsOfType<ClickTarget>();
         for ( int i = 0 ; i < targets.Length ; i++ )
         {
             targets[i].OnClicked += OnTargetClicked;
         }
     }
 
     private void OnTargetClicked( ClickTarget target )
     {
         CurrentTarget = target.gameObject;
     }
 }

  1. Attach the following ClickTarget script to the objects that must be clicked. Don't forget to add a Collider to each of them before


 using UnityEngine;
 using UnityEngine.EventSystems;
 
 [RequireComponent(typeof(Collider))]
 public class ClickTarget : MonoBehaviour, IPointerClickHandler
 {
     public System.Action<ClickTarget> OnClicked;
 
     public void OnPointerClick( PointerEventData eventData )
     {
         if ( OnClicked != null )
             OnClicked.Invoke( this );
     }
 }
Comment
Add comment · Show 3 · 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 Astha_Mishra · Feb 02, 2018 at 02:00 PM 0
Share

Actually i m working with 2D

avatar image Hellium Astha_Mishra · Feb 03, 2018 at 10:20 AM 0
Share

Then, attach a Physics2DRaycaster ins$$anonymous$$d of a PhysicsRaycaster and attach Collider2D components ins$$anonymous$$d of simple Colliders

avatar image Hellium Astha_Mishra · Feb 06, 2018 at 10:50 AM 0
Share

Did the script work with the Physics2DRaycaster and Collider2D?

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

177 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 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 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

Unity5/C# beginner here! Need some scripting help :) <3 1 Answer

Destroy(this.gameObject) destroys all the obejcts in the scene, i want only the one that is clicked on 1 Answer

Animation won't stop (Solved) 2 Answers

Grenade damage script error 0 Answers

Many objects with decimal place in the millionths, can this cause any performance issues? 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