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 JackQuinton · Jun 26, 2017 at 04:32 PM · c#methodcallvoid

How can I write: if this method (X) is activated by another specific method (Y) do this (z)

In this example x is CloseBuildingTree and I have a sound i want to play when it closes but it also closes when Y 'buildtower' is activated and I have another sound for that situation.

How can I write that 'tree close sound' isn't logged when closebuildingtree is activated by buildtower?

   private void CloseBuildingTree()
         {
             if (activeBuildingTree != null)
             {
                 Destroy(activeBuildingTree.gameObject);
                 // Enable tower raycast
                 bodyCollider.enabled = true;
     
     
                 Debug.Log("tree close sound");
             }
     
             
     
             
         }
     
         /// <summary>
         /// Builds the tower.
         /// </summary>
         /// <param name="towerPrefab">Tower prefab.</param>
         public void BuildTower(GameObject towerPrefab)
         {
             // Close active building tree
             CloseBuildingTree();
             Price price = towerPrefab.GetComponent<Price>();
             // If anough gold
             if (uiManager.SpendGold(price.price) == true)
             {
                 // Create new tower and place it on same position
                 GameObject newTower = Instantiate<GameObject>(towerPrefab, transform.parent);
                 newTower.transform.position = transform.position;
                 newTower.transform.rotation = transform.rotation;
                 // Destroy old tower
                 Destroy(gameObject);
             }
     
     
         }





 

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 JackQuinton · Jun 28, 2017 at 07:35 PM 0
Share

So it appears that when I build a tower it runs both the build tower to close method and the click off one bellow:

     private void UserClick(GameObject obj, string param)
     {
         if (obj == gameObject) // This tower is clicked
         {
             // Show attack range
             ShowRange(true);
             if (activeBuildingTree == null)
             {
                 // Open building tree if it is not
                 OpenBuildingTree();
             }
         }
         else // Other click
         {
             // Hide attack range
             ShowRange(false);
             // Close active building tree
             CloseBuildingTree(true);
 
          
     
             
         }


I tried something like this but I'm not sure how to write it, any tips?

https://gist.github.com/anonymous/85bb165682e1cee9a65fdd4c4b889686

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Hellium · Jun 26, 2017 at 04:38 PM

      private void CloseBuildingTree( bool calledByBuildTower )
      {
          if (activeBuildingTree != null)
          {
              Destroy(activeBuildingTree.gameObject);
              // Enable tower raycast
              bodyCollider.enabled = true;
  
              if( calledByBuildTower )
                     Debug.Log("tree close sound");
          }

      private void CloseBuildingTree()
      {
          CloseBuildingTree( false ) ;
      }

      public void BuildTower(GameObject towerPrefab)
      {
          // Close active building tree
          CloseBuildingTree( true );
          Price price = towerPrefab.GetComponent<Price>();
          // If anough gold
          if (uiManager.SpendGold(price.price) == true)
          {
              // Create new tower and place it on same position
              GameObject newTower = Instantiate<GameObject>(towerPrefab, transform.parent);
              newTower.transform.position = transform.position;
              newTower.transform.rotation = transform.rotation;
              // Destroy old tower
              Destroy(gameObject);
          }
  
  
      }

Comment
Add comment · Show 9 · 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 JackQuinton · Jun 26, 2017 at 06:52 PM 0
Share

Hey thanks, thats in the right direction to where I want to go but it's doing what I want in reverse. I want the debug to not be called when 'buildtower' activates the method. how can I change it?

avatar image Hellium JackQuinton · Jun 26, 2017 at 06:59 PM 0
Share

Simply change the condition inside the CloseBuildingTree function :

 if( !calledByBuildTower )
                  Debug.Log("tree close sound");
avatar image EpsilonQoppa Hellium · Jun 26, 2017 at 10:05 PM 0
Share

Helllium's a baller!

Show more comments
avatar image JackQuinton · Jun 27, 2017 at 12:34 PM 0
Share

Sorry for another comment but adding if( !calledByBuildTower ) Debug.Log("tree close sound");

gives me the debug message both ways again. any ideas?

avatar image Hellium JackQuinton · Jun 27, 2017 at 09:41 PM 0
Share

You should check the Trace in the console to find which function called the CloseBuildingTree function.

avatar image JackQuinton · Jun 27, 2017 at 07:25 PM 0
Share

I've recorded it to make it a bit easier to understand I hope https://streamable.com/fmart

avatar image JackQuinton · Jun 29, 2017 at 10:38 AM 0
Share

So using what you said it appears that when I build a tower it activates both the build tower method and the 'click off to close' method. That being the case is it possible to say when tower build and tower click off methods are activated with the same click, don't play a sound in tower click off?

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

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

OnTriggerEnter2D(collidertype coll) C# 2 Answers

How can I call a method from another script? 3 Answers

calling an void from another script and passing paramters... 1 Answer

Check if a function is no longer being called? 3 Answers

Multiple Cars not working 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