Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How to set the None mode from the enum if the method that use switch/case type of IEnumerator ?

Discussion in 'Scripting' started by Chocolade, May 13, 2022.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    In the method SelectAnimation i want that if i change the mode to None then set the linerenderer color to Red either before running the game or while the game is running.

    The problem is that the method SelectAnimation get IEnumerator.

    I could use the default state now it's setting to null but i'm not sure how to do it.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. [RequireComponent(typeof(LineRenderer))]
    8. public class LineRendererColors : MonoBehaviour
    9. {
    10.    public enum AnimationType { None, SingleColorMorph, MultiColorMorph, Shuffle, Shift };
    11.    LineRenderer myLineRenderer;
    12.    Coroutine _animationInProgress;
    13.    public float morphTime;
    14.  
    15.    [SerializeField]
    16.    AnimationType _animationType;
    17.  
    18. #if UNITY_EDITOR
    19.    AnimationType _cachedAnimationType;
    20.    void OnValidate()
    21.    {
    22.        if (_animationType != _cachedAnimationType)
    23.        {
    24.            if (_animationInProgress == null)
    25.                _cachedAnimationType = _animationType;
    26.            else
    27.                StartAnimation(_animationType);
    28.        }
    29.    }
    30. #endif
    31.  
    32.    void StartAnimation(AnimationType animType)
    33.    {
    34.        _animationType = animType;
    35. #if UNITY_EDITOR
    36.        _cachedAnimationType = animType;
    37. #endif
    38.  
    39.        if (_animationInProgress != null)
    40.            StopCoroutine(_animationInProgress);
    41.  
    42.        _animationInProgress = StartCoroutine(SelectAnimation(animType));
    43.    }
    44.  
    45.    IEnumerator SelectAnimation(AnimationType animType)
    46.    {
    47.        switch (animType)
    48.        {
    49.            case AnimationType.SingleColorMorph:
    50.                return RandomSingleColorMorphing(myLineRenderer, morphTime);
    51.            case AnimationType.MultiColorMorph:
    52.                return RandomMultiColorMorphing(myLineRenderer, morphTime);
    53.            case AnimationType.Shuffle:
    54.                return ShuffleGradient(myLineRenderer, .5f);
    55.            case AnimationType.Shift:
    56.                return AnimateLoop(myLineRenderer);
    57.            default:
    58.                return null;
    59.        }
    60.    }
    61.  
    62.    void Start()
    63.    {
    64.        myLineRenderer = GetComponent<LineRenderer>();
    65.        StartAnimation(_animationType);
    66.    }
    67.  
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,298
    It seems the class has direct access to myLineRenderer, so why not just use that to change the color?
    The None value is nothing special, you can just add it to the switch in SelectAnimation and do whatever you like.
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    but the SelectAnimation return IEnumerator if i'm using the None to set a color what should i return ? I can't just return a simple void method it must be IEnumerator.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    All of your
    return
    statements in that method should be
    yield return
    . Once you've done that:

    You could do
    yield break;
    but it would be better in this case to just delete your default case entirely since it does nothing.
     
    Chocolade likes this.
  5. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    Cannot be yield return give error.



    All the errors are like :

    Control cannot fall through from one case label ('case AnimationType.SingleColorMorph:') to another

    I also deleted after it the default but still all this lines give the same error. and still not sure how to use the None here.
    Could you please show me what and how to do it ?
     
  6. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    This is is a link for the complete script before i did this changes with the errors :

    https://pastebin.com/0KHLywhp
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Those are unrelated errors. You need break; in each switch case in C#
     
    Chocolade likes this.
  8. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    Working thanks.