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 AE811 · Jan 23, 2017 at 12:43 PM · componentgameobjectsclones

Get access of just one clone's component in a script attached to that clone

Hi! I'm very new at this. I've been trying to get to the components of a clone but it keeps returning all the component of all the instantiated clones instead of the one the script is attached to.

This is the script that is attached to the objects.


using System.Collections; using System.Collections.Generic; using UnityEngine;

namespace Completed { public class CompareCards : MonoBehaviour {

 [HideInInspector]public Sprite m_SelectedCard;

 // Update is called once per frame
 void Update () {

         if (Input.GetKeyDown ("mouse 0")) {
             GetSelectedCard ();

         }
     
 }

     void GetSelectedCard() {
         Sprite selectedCard = m_SelectedCard;


         selectedCard = gameObject.GetComponent<SpriteRenderer>().sprite;
         Debug.Log ("m_SelectedCard in CompareCards: " + selectedCard);
     }



Debug.Log shows mee that it get's the sprite of all the clones and not just the one the script is attached to. I need to select one so I can compare it to an other.

Thanks for any help you can offer!

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 AE811 · Jan 26, 2017 at 06:58 AM 0
Share

To Elaborate. I need to get to the gameobjects component using the script attached to the gameobject. (script attached to a button with a sprite)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UDM1983 · Jan 26, 2017 at 11:33 AM

Since all the clones have this script on them, when you press the mouse 0 button, all scripts on all objects run at once, thus why you get this. To fix it, use a bool like this:

 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 
 namespace Completed { 
 public class CompareCards : MonoBehaviour {
  [HideInInspector]public Sprite m_SelectedCard;
 // Only the current selected card should have isSelected true. 
 public bool isSelected = false;  
 
  // Update is called once per frame
  void Update () {
 
          if (Input.GetKeyDown ("mouse 0")) {
              GetSelectedCard ();
 
          }   
  }
 
      void GetSelectedCard() {
         Sprite selectedCard = m_SelectedCard;
    
         if (isSelected)
         { 
             selectedCard = gameObject.GetComponent<SpriteRenderer>().sprite;
             Debug.Log ("m_SelectedCard in CompareCards: " + selectedCard);
         }
      }


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 AE811 · Jan 26, 2017 at 12:48 PM 1
Share

Thanks for trying to help! I finally managed to get it to work with "button.onClick.AddListner(function)". I also had to add an image(script) to my button to make it able to register the click. It didn't have one since I started out with a spimple sprite ins$$anonymous$$d of a button. Rooky mistake, I'm sure but this is my first attempt at creating something myself.

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

how do I reference a clones of a instantiated game object and only the clones In a script? 1 Answer

Sharing components amongst different gameobjects 1 Answer

Storing the status of a GameObject. 2 Answers

extra GameObjects Spawning 2 Answers

2D Animation does not start 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