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

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[RequireComponent(typeof(LineRenderer))]
public class LineRendererColors : MonoBehaviour
{
   public enum AnimationType { None, SingleColorMorph, MultiColorMorph, Shuffle, Shift };
   LineRenderer myLineRenderer;
   Coroutine _animationInProgress;
   public float morphTime;

   [SerializeField]
   AnimationType _animationType;

#if UNITY_EDITOR
   AnimationType _cachedAnimationType;
   void OnValidate()
   {
       if (_animationType != _cachedAnimationType)
       {
           if (_animationInProgress == null)
               _cachedAnimationType = _animationType;
           else
               StartAnimation(_animationType);
       }
   }
#endif

   void StartAnimation(AnimationType animType)
   {
       _animationType = animType;
#if UNITY_EDITOR
       _cachedAnimationType = animType;
#endif

       if (_animationInProgress != null)
           StopCoroutine(_animationInProgress);

       _animationInProgress = StartCoroutine(SelectAnimation(animType));
   }

   IEnumerator SelectAnimation(AnimationType animType)
   {
       switch (animType)
       {
           case AnimationType.SingleColorMorph:
               return RandomSingleColorMorphing(myLineRenderer, morphTime);
           case AnimationType.MultiColorMorph:
               return RandomMultiColorMorphing(myLineRenderer, morphTime);
           case AnimationType.Shuffle:
               return ShuffleGradient(myLineRenderer, .5f);
           case AnimationType.Shift:
               return AnimateLoop(myLineRenderer);
           default:
               return null;
       }
   }

   void Start()
   {
       myLineRenderer = GetComponent<LineRenderer>();
       StartAnimation(_animationType);
   }

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.

1 Like

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.

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.

1 Like

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 ?

This is is a link for the complete script before i did this changes with the errors :

https://pastebin.com/0KHLywhp

Those are unrelated errors. You need break; in each switch case in C#

1 Like

Working thanks.