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 /
  • Help Room /
avatar image
0
Question by NAYIR55 · Jul 05, 2016 at 12:30 AM · c#not workingenumhow

[C#] Enum not working properly

Hello

I'm working on a script to fade UI elements in Unity, similar to a selector, where you can select the type of fading, and duration, and image to fade

My problem is when I run the code only one enum statement work and the other don't, no matter if I use switch or if just the first statement run, I don't know what's wrong with the code

  • Please explain your answer

  • Please explain how the code is wrong

  • Please give feedback on how to improve

I'm using Unity version 5.3.5f1 and Visual Studio Community 2015

Goal

  • Make the enum work properly using either switch or if

  • Be able to use the variables inside the FadeOperations class to make the calculations inside the Test class

  • Select from an array the type of desired operation

  • Select an UI element from Heriarchy and fade it

Code

Here's my code so far

 using UnityEngine;
 using UnityEngine.UI;
 
 public enum FadeManager
 {
     fadeIn,
     fadeOut
 };
 
 [System.Serializable]
 public class FadeOperations
 {
     [Tooltip("Type of fading")]
     public FadeManager fadeType;
 
     [Tooltip("Duration time of the fading")]
     public float duration;
 
     [Tooltip("Select the image to fade")]
     public Image fadeImage;
 }
 
 public class Test : MonoBehaviour
 {
     [Tooltip("Select your type of fade")]
     public FadeOperations[] fadeOperations;
 
     //Reference to the class FadeOperations
     private FadeOperations _fo = new FadeOperations();
 
     //Loop for debug
     private void Update()
     {
         switch (_fo.fadeType)
         {
             //This statement works
             case FadeManager.fadeIn:
                 Debug.Log("Fadein"); //Only this piece of code works
                 break;
 
             //This statement doesn't work
             case FadeManager.fadeOut:
                 Debug.Log("Fadeout");
                 break;
         }
      }
  }
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 Dudicus · Jul 05, 2016 at 02:01 AM

You are trying to use a list as an enum. If you add [] at the end of the class name while declaring an instance it makes it a list. Remove that and change it to an enum. I don't know the actual code to make it a class an enum or even if that is possible but all the other lines of code should work.

If you become too stuck on this make a separate code for each GameObject.

I know I didn't answer your question but I hope it helps!!!

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 NoseKills · Jul 05, 2016 at 07:21 AM

When do you expect the other branch in the switch to trigger?

You make an instance of FadeOperations to a private field _fo and that instance is what you are checking in the switch. That instance will always have the default value for fadeType since you don't change it anywhere.

      private FadeOperations _fo = new FadeOperations();
      private void Update()
      {
          switch (_fo.fadeType)
          {...

If you want to check one of the FadeOperations instances to which you set a different fadeType to in the inspector, I believe you want to pull it from the public array you have (make sure you have at least one item in it)

      private void Update()
      {
          switch (fadeOperations[0].fadeType)
          {...
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.

Follow this Question

Answers Answers and Comments

178 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

Related Questions

My public enum isn't functioning like I want it to 0 Answers

Problem assigning enum value 1 Answer

[noob] I have a script that theoretically should manage movement of a 2D GameObject... 1 Answer

I want my code to tell the AI to move from one log then back to the fire chamber and then to the other log but it just moves to the first log and doesnt do anything did i code something wrong? 2 Answers

IENumerator does not work 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