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 20, 2017 at 10:47 AM · c#gameobjectbasicbasic programming

How to reference a public gameobjects in script which are attached in the inspector. (solved)

I'm a sound designer so I'm currently logging debugs for where my event triggers will go. short description of what i'm trying to do:

I have two spells with cool downs and want a different sound for each one when they finish the cool down but the spells use the same "user action" script (pictured) with public variables changed in the inspector like the countdown time or prefab effect.

How can achieve this? i've included some of the script which relates to the ending off the cool down and a commented out part where I attempted to solve it but it kind of describes what I want it to do.

     void Update()
     {
         if (myState == MyState.Cooldown)
         {
             if (cooldownCounter > 0f)
             {
                 cooldownCounter -= Time.deltaTime;
                 UpdateCooldownText();
             }
             else if (cooldownCounter <= 0f)
             {
                 StopCooldown();
 
                 Debug.Log("cooldown over but this shows for both spells ");
 
                 //If the public gameobject "useractionprefab" in the inspector is "starburst" when the countdown finishes then play that sound
 
                 /*if (userActionPrefab = ("StarBurst")
                 {
                     Debug.Log("cooldown over for starburst spell");
 
                 } */
             }
         }
     }



alt text

screen-shot-2017-06-20-at-113219.png (14.4 kB)
screen-shot-2017-06-20-at-113229.png (14.5 kB)
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

2 Replies

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

Answer by JackQuinton · Jun 20, 2017 at 03:42 PM

I figured it out by myself. If there is an easier way of doing this please let me know.

 void Update()
       {
           if (myState == MyState.Cooldown)
           {
               if (cooldownCounter > 0f)
               {
                   cooldownCounter -= Time.deltaTime;
                   UpdateCooldownText();
               }
               else if (cooldownCounter <= 0f)
               {
                   StopCooldown();
   
                   Debug.Log("cooldown over but this shows for both spells ");
 
                   if (userActionPrefab.name.Equals("FireStorm"))
 
                         { 
 
                         Debug.Log("timer reup for fire storm");
                         }
                           

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

Answer by JxWolfe · Jun 20, 2017 at 11:40 AM

      void Update()
      {
          if (myState == MyState.Cooldown)
          {
              if (cooldownCounter > 0f)
              {
                  cooldownCounter -= Time.deltaTime;
                  UpdateCooldownText();
              }
              else if (cooldownCounter <= 0f)
              {
                  StopCooldown();
  
                  Debug.Log("cooldown over but this shows for both spells ");
  
                  //If the public gameobject "useractionprefab" in the inspector is "starburst" when the countdown finishes then play that sound
  //             // ask if the userActionPrefab script if is starburst or not
                  if (userActionPrefab.GetComponent(UserActionPrefab).isStarburst)
                  {
                      Debug.Log("cooldown over for starburst spell");
  
                  } 
                  else{ //is the other spell
                    Debug.Log("cooldown over for starburst spell");
  
                   }
               }
          }
      }


That would work if you added a public bool called "isStarburst".

Edit for any more then two:

oops, in the brackets you would need the userActionPrefab's script, and if you had more then two spells then i would have an enum and use a switch statement like this small code:

 [System.Serializable]
 public enum Spells {Starburst,Spell2,Spell3}
 //public enum Spells {starburst,spell2, spell3} //so on
 
 //the script that is sending the spell not "Test"
 public class Test : MonoBehaviour {
     
     public Spells spells;

then you would get it with a switch statement that is like this:

 switch (spells) {
         case Spells.Starburst:
             {
                 //is a starburst
 
                 //then after you are finished with you code you have to have this line
                 break;
             }
         case Spells.Spell2:
             {
                 //is another spell
 
                 break;
             }
         case Spells.Spell3:
             {
                 //so on
 
                 break;
             }

that code only works from the "Test script" you would have to edit it for the other scripts but that's the idea

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 JackQuinton · Jun 20, 2017 at 01:01 PM 0
Share

Thanks for the help. Would you know how to do this if I had more than two spells?

avatar image JackQuinton · Jun 20, 2017 at 01:24 PM 0
Share

I copied in your script and added a "public bool isStarburst" but i'm getting these errors. I guessed it could of been because the UserActionPrefab starting with a capital but I still got an error when I changed it. Any ideas? alt text

screen-shot-2017-06-20-at-142006.png (79.0 kB)
screen-shot-2017-06-20-at-142232.png (104.8 kB)
avatar image JxWolfe JackQuinton · Jun 20, 2017 at 05:16 PM 0
Share

oops, it is the script you added the bool, so the name of the code on userActionPrefab

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

335 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

Related Questions

NPC Class looking for help 1 Answer

Multiple Cars not working 1 Answer

Cliks per second does not work 1 Answer

Passing a variable to Start() - C# 2 Answers

Distribute terrain in zones 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