• 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 OrionCulver · May 12, 2018 at 05:42 PM · prefabclickinstance

Yet another prefab question

Hi,

I made a prefab button. the prefab has 3 images that represent state. the prefab has a script with public images so that the prefab has access to the image object via the inspector window. the prefab's script also has an OnClick method that shows/hides the state images in a cycle.

what I want to do is allow instances of the prefab use the OnClick method in the prefab's script to handle changing the visibility of the state images, but for the clicked instance only.

how would I go about doing this?

TIA

Below is the code in the script attached to the prefab.

     using TMPro;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class TrackButton : MonoBehaviour {
     
         public TrackButtonState CurrentState;
         public Image SelectionImage;
         public Image PlayingImage;
         public Image PausedImage;
         public TextMeshProUGUI ArtistName;
         public TextMeshProUGUI TrackName;
     
         // Use this for initialization
         public void Start () {
             SetUnselectedState();
         }
         
         // Update is called once per frame
         public void Update () {
             
         }
     
         public void OnClick() {
             switch (CurrentState)
             {
                 case TrackButtonState.Unselected:
                     {
                         SetSelectedState();
                         break;
                     }
                 case TrackButtonState.Selected:
                     {
                         SetPlayingState();
                         break;
                     }
                 case TrackButtonState.Playing:
                     {
                         SetPausedState();
                         break;
                     }
                 case TrackButtonState.Paused:
                     {
                         SetUnselectedState();
                         break;
                     }
             }
         }
     
         public void SetUnselectedState()
         {
             CurrentState = TrackButtonState.Unselected;
             SelectionImage.gameObject.SetActive(true);
             PlayingImage.gameObject.SetActive(false);
             PausedImage.gameObject.SetActive(false);
         }
     
         public void SetSelectedState()
         {
             CurrentState = TrackButtonState.Selected;
             SelectionImage.gameObject.SetActive(false);
             PlayingImage.gameObject.SetActive(true);
             PausedImage.gameObject.SetActive(false);
         }
     
         public void SetPlayingState()
         {
             CurrentState = TrackButtonState.Playing;
             SelectionImage.gameObject.SetActive(false);
             PlayingImage.gameObject.SetActive(true);
             PausedImage.gameObject.SetActive(false);
         }
     
         public void SetPausedState()
         {
             CurrentState = TrackButtonState.Paused;
             SelectionImage.gameObject.SetActive(false);
             PlayingImage.gameObject.SetActive(false);
             PausedImage.gameObject.SetActive(true);
         }
     }
     
 
Comment
Add comment · Show 5
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 iJuan · May 12, 2018 at 05:58 PM 0
Share

When you instantiate a copy of a prefab, you are also creating an instance of the class which is attached to that prefab, so everything stored locally will do what you want

avatar image OrionCulver iJuan · May 12, 2018 at 06:14 PM 0
Share

then why don't the instances in the scene change with I click them?

avatar image iJuan iJuan · May 12, 2018 at 06:24 PM 0
Share

You are targeting to images in defined externally, there is your problem. You should treat images as images, not as game objects. Add an Image (script) component to your prefab, and modify the current image on that component to control what's being displayed.

Also, there's a 'Button' component in Unity

avatar image OrionCulver · May 14, 2018 at 06:31 AM 0
Share
 using T$$anonymous$$Pro;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TrackButton : $$anonymous$$onoBehaviour {
 
     public TrackButtonState CurrentState;
     public Image SelectionImage;
     public Image PlayingImage;
     public Image PausedImage;
     public Text$$anonymous$$eshProUGUI ArtistName;
     public Text$$anonymous$$eshProUGUI TrackName;
 
     // Use this for initialization
     public void Start () {
         SetUnselectedState();
     }
     
     // Update is called once per frame
     public void Update () {
         
     }
 
     public void OnClick() {
         switch (CurrentState)
         {
             case TrackButtonState.Unselected:
                 {
                     SetSelectedState();
                     break;
                 }
             case TrackButtonState.Selected:
                 {
                     SetPlayingState();
                     break;
                 }
             case TrackButtonState.Playing:
                 {
                     SetPausedState();
                     break;
                 }
             case TrackButtonState.Paused:
                 {
                     SetUnselectedState();
                     break;
                 }
         }
     }
 
     public void SetUnselectedState()
     {
         CurrentState = TrackButtonState.Unselected;
         SelectionImage.gameObject.SetActive(true);
         PlayingImage.gameObject.SetActive(false);
         PausedImage.gameObject.SetActive(false);
     }
 
     public void SetSelectedState()
     {
         CurrentState = TrackButtonState.Selected;
         SelectionImage.gameObject.SetActive(false);
         PlayingImage.gameObject.SetActive(true);
         PausedImage.gameObject.SetActive(false);
     }
 
     public void SetPlayingState()
     {
         CurrentState = TrackButtonState.Playing;
         SelectionImage.gameObject.SetActive(false);
         PlayingImage.gameObject.SetActive(true);
         PausedImage.gameObject.SetActive(false);
     }
 
     public void SetPausedState()
     {
         CurrentState = TrackButtonState.Paused;
         SelectionImage.gameObject.SetActive(false);
         PlayingImage.gameObject.SetActive(false);
         PausedImage.gameObject.SetActive(true);
     }
 }
 
avatar image OrionCulver · May 14, 2018 at 06:31 AM 0
Share
     using T$$anonymous$$Pro;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class TrackButton : $$anonymous$$onoBehaviour {
     
         public TrackButtonState CurrentState;
         public Image SelectionImage;
         public Image PlayingImage;
         public Image PausedImage;
         public Text$$anonymous$$eshProUGUI ArtistName;
         public Text$$anonymous$$eshProUGUI TrackName;
     
         // Use this for initialization
         public void Start () {
             SetUnselectedState();
         }
         
         // Update is called once per frame
         public void Update () {
             
         }
     
         public void OnClick() {
             switch (CurrentState)
             {
                 case TrackButtonState.Unselected:
                     {
                         SetSelectedState();
                         break;
                     }
                 case TrackButtonState.Selected:
                     {
                         SetPlayingState();
                         break;
                     }
                 case TrackButtonState.Playing:
                     {
                         SetPausedState();
                         break;
                     }
                 case TrackButtonState.Paused:
                     {
                         SetUnselectedState();
                         break;
                     }
             }
         }
     
         public void SetUnselectedState()
         {
             CurrentState = TrackButtonState.Unselected;
             SelectionImage.gameObject.SetActive(true);
             PlayingImage.gameObject.SetActive(false);
             PausedImage.gameObject.SetActive(false);
         }
     
         public void SetSelectedState()
         {
             CurrentState = TrackButtonState.Selected;
             SelectionImage.gameObject.SetActive(false);
             PlayingImage.gameObject.SetActive(true);
             PausedImage.gameObject.SetActive(false);
         }
     
         public void SetPlayingState()
         {
             CurrentState = TrackButtonState.Playing;
             SelectionImage.gameObject.SetActive(false);
             PlayingImage.gameObject.SetActive(true);
             PausedImage.gameObject.SetActive(false);
         }
     
         public void SetPausedState()
         {
             CurrentState = TrackButtonState.Paused;
             SelectionImage.gameObject.SetActive(false);
             PlayingImage.gameObject.SetActive(false);
             PausedImage.gameObject.SetActive(true);
         }
     }
     
 

1 Reply

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

Answer by OrionCulver · May 12, 2018 at 06:20 PM

Dang it - all I had to do was add the following inside the Start() method:

GetComponent().onClick.AddListener(OnClick);

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.

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

106 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

Related Questions

How do I re-link an object that has unintentionally become detached from prefab? 0 Answers

Script uses transform.position of Prefab instead of Instance 1 Answer

How (and where) does Unity save the modified parameters of a prefab's instance? 1 Answer

Save a gameobject as prefab on button press.,Is there a way for the player to create a prefab with a button press? 3 Answers

How do I count how many instances of a prefab there are? 3 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