• 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 /
This question was closed yesterday by Karma_XX for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Karma_XX · 5 days ago · unity 2dbeginnervoid

(solved) How to stop a void

(I'm new to coding and to Unity) The following code keeps on repeating the PasteClicked void even tho I clicked the button to start CopyClicked void. I would like that my code start the PasteClicked or CopyClicked void only once and if the button is reclicked it re-start it only once.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class AddorCopy : MonoBehaviour
 {
     public Button paste;
     public Button copy;
     public Material presets1;
     public Material presets2;
     public Material presets3;
     public Material presets4;
     public Material presets5;
     public Material presets6;
     public Material Base_material;
     public Button button1;
     public Button button2;
     public Button button3;
     public Button button4;
     public Button button5;
     public Button button6;
 
     public void Start()
     {
         paste.onClick.AddListener(PasteClicked);
         copy.onClick.AddListener(CopyClicked);
 
         void PasteClicked()
         {
             button1.onClick.AddListener(one);
             button2.onClick.AddListener(two);
             button3.onClick.AddListener(three);
             button4.onClick.AddListener(four);
             button5.onClick.AddListener(five);
             button6.onClick.AddListener(six);
             void one()
             {
                 presets1.CopyPropertiesFromMaterial(Base_material);
             }
             void two()
             {
                 presets2.CopyPropertiesFromMaterial(Base_material);
             }
             void three()
             {
                 presets3.CopyPropertiesFromMaterial(Base_material);
             }
             void four()
             {
                 presets4.CopyPropertiesFromMaterial(Base_material);
             }
             void five()
             {
                 presets5.CopyPropertiesFromMaterial(Base_material);
             }
             void six()
             {
                 presets6.CopyPropertiesFromMaterial(Base_material);
             }
         }
 
         void CopyClicked()
         {
             button1.onClick.AddListener(one);
             button2.onClick.AddListener(two);
             button3.onClick.AddListener(three);
             button4.onClick.AddListener(four);
             button5.onClick.AddListener(five);
             button6.onClick.AddListener(six);
             void one()
             {
                 Base_material.CopyPropertiesFromMaterial(presets1);
             }
             void two()
             {
                 Base_material.CopyPropertiesFromMaterial(presets2);
             }
             void three()
             {
                 Base_material.CopyPropertiesFromMaterial(presets3);
             }
             void four()
             {
                 Base_material.CopyPropertiesFromMaterial(presets4);
             }
             void five()
             {
                 Base_material.CopyPropertiesFromMaterial(presets5);
             }
             void six()
             {
                 Base_material.CopyPropertiesFromMaterial(presets6);
             }
 
         }
     }
 
 }

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

  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · 5 days ago

ADDING a listener, as its name suggests, ADDS a listener. It does not remove the previous callbacks.

I also recommend you taking advantage of arrays and classes to tidy your code up.

CODE NOT TESTED

 public class AddorCopy : MonoBehaviour
 {
     public Button paste;
     public Button copy;
     public Material baseMaterial;
     public MaterialPresetButton[] materialPresetButtons;
 
     void Start()
     {
         paste.onClick.AddListener(PasteClicked);
         copy.onClick.AddListener(CopyClicked);
 
         for(int i = 0 ; i < materialPresetButtons.Length ; ++i)
         {
             materialPresetButtons[i].Setup(baseMaterial);
         }
     }
 
     void PasteClicked()
     {
         for(int i = 0 ; i < materialPresetButtons.Length ; ++i)
         {
             materialPresetButtons[i].SwitchToPasteMode();
         }
     }
 
     void CopyClicked()
     {
         for(int i = 0 ; i < materialPresetButtons.Length ; ++i)
         {
             materialPresetButtons[i].SwitchToCopyMode();
         }
     }
 }
 
 [System.Serializable]
 public class MaterialPresetButton
 {
     [SerializeField] private Button button;
     [SerializeField] private Material preset;
     private Material baseMaterial;
     private Material source;
     private Material target;
 
     public void Setup(Material baseMaterial)
     {
         this.baseMaterial = baseMaterial;
 
         button.onClick.AddListener(CopyMaterialProperties);
 
         SwitchToCopyMode();
     }
 
     public void SwitchToCopyMode()
     {
         source = baseMaterial;
         target = preset;
     }
     
     public void SwitchToPasteMode()
     {
         source = preset;
         target = baseMaterial;
     }
 
     private void CopyMaterialProperties()
     {
         target.CopyPropertiesFromMaterial(source);
     }
 }
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 Karma_XX · 5 days ago 0
Share

Thank you a lot it works! you saved me :D (the line 56-57 and 62-63 are inverted but I fixed it)

Follow this Question

Answers Answers and Comments

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

My knockback based on physics doesn't work, would you help me fix it? 0 Answers

unexpected void error 1 Answer

Input System Not Recognized? 0 Answers

How to create a 2D slot game in unity step wise step as same as in jetpack joyride? -2 Answers

Ladder script doesn't work no errors 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