Search Unity

How can i change direction each time one direction ?

Discussion in 'Scripting' started by haimmoshe, Nov 21, 2017.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class LightsEffects : MonoBehaviour
    8. {
    9.    private Renderer[] renderers;
    10.    private float lastChangeTime;
    11.    private int greenIndex = 0;
    12.  
    13.    public void LightsEffect(List<GameObject> objects, Color instantiateColor, Color colorEffect)
    14.    {
    15.        renderers = new Renderer[objects.Count];
    16.        for (int i = 0; i < renderers.Length; i++)
    17.        {
    18.            renderers[i] = objects[i].GetComponent<Renderer>();
    19.            renderers[i].material.color = Color.red;
    20.        }
    21.  
    22.        // Set green color to the first one
    23.        greenIndex = 0;
    24.        renderers[greenIndex].material.color = Color.green;
    25.    }
    26.  
    27.    /// <summary>
    28.    /// Running the effect
    29.    /// </summary>
    30.    /// <param name="changeDirection">Changing the lights movement directions - false = forward, true = backward.</param>
    31.    public void LightsEffectCore(float delay, bool changeDirection)
    32.    {
    33.        // Change color each `delay` seconds
    34.        if (Time.time > lastChangeTime + delay)
    35.        {
    36.            lastChangeTime = Time.time;
    37.  
    38.            // Set color of the last renderer to red
    39.            // and the color of the current one to green
    40.            renderers[greenIndex].material.color = Color.red;
    41.            if (changeDirection == true)
    42.            {
    43.                Array.Reverse(renderers);
    44.                changeDirection = false;
    45.            }
    46.            greenIndex = (greenIndex + 1) % renderers.Length;
    47.            renderers[greenIndex].material.color = Color.green;
    48.        }
    49.    }
    50. }
    51.  
    The part of the direction change:

    Code (csharp):
    1.  
    2. if (changeDirection == true)
    3.                {
    4.                    Array.Reverse(renderers);
    5.                    changeDirection = false;
    6.                }
    7.  
    The problem is that now it will be true all the time and next time it will be changed to false i will need to reverse the array again but then since it's false it will reverse the array all the time:

    Code (csharp):
    1.  
    2. if (changeDirection == true)
    3.            {
    4.                Array.Reverse(renderers);
    5.                changeDirection = false;
    6.            }
    7.            else
    8.            {
    9.                Array.Reverse(renderers);
    10.                changeDirection = true;
    11.            }
    12.  
    But now when it will be change back to false or if the game start as false it will change right back to true and will reverse the array and so on.

    I want that when it's true keep it true and once changing it to false keep it false until changing again. I got messed the flag states and the reversing.

    So i added a new flag variable now at the top of the script:

    Code (csharp):
    1.  
    2. private bool directionChanged = false;
    3.  
    But not sure how to use it in the part of the direction states false/true.
     
  2. Nitrousek

    Nitrousek

    Joined:
    Jan 31, 2016
    Posts:
    39
    Code (CSharp):
    1. if(shouldChangeDirection)
    2. {
    3. Array.Reverse(renderers);
    4. isReversed = !isReversed;
    5.  }
    now you keep track whether it is reversed - and you can manipulate "shouldChangeDirection" whether you should or should not change the direction, by whatever measure you want.
     
    haimmoshe likes this.
  3. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    The problem is that i'm using this script in another script.
    In the other script:

    Code (csharp):
    1.  
    2. public LightsEffects lightseffect;
    3. public bool changeLightsDirection = false;
    4.  
    Then in Update:

    Code (csharp):
    1.  
    2. lightseffect.LightsEffectCore(0.1f, changeLightsDirection);
    3.  
    What i want is from this script to change the direction in LightsEffect.
    But if i'm doing in LightsEffect:

    [code[
    if(shouldChangeDirection)
    [/code]

    Then it will be all the time true.

    This is how the LightsEffect script now:

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class LightsEffects
    8. {
    9.     private Renderer[] renderers;
    10.     private float lastChangeTime;
    11.     private int greenIndex = 0;
    12.     private bool shouldChangeDirection = false;
    13.  
    14.     public void LightsEffect(List<GameObject> objects, Color instantiateColor, Color colorEffect)
    15.     {
    16.         renderers = new Renderer[objects.Count];
    17.         for (int i = 0; i < renderers.Length; i++)
    18.         {
    19.             renderers[i] = objects[i].GetComponent<Renderer>();
    20.             renderers[i].material.color = Color.red;
    21.         }
    22.  
    23.         // Set green color to the first one
    24.         greenIndex = 0;
    25.         renderers[greenIndex].material.color = Color.green;
    26.     }
    27.  
    28.     /// <summary>
    29.     /// Running the effect
    30.     /// </summary>
    31.     /// <param name="isReversed">Changing the lights movement directions - false = forward, true = backward.</param>
    32.     public void LightsEffectCore(float delay, bool isReversed)
    33.     {
    34.         // Change color each `delay` seconds
    35.         if (Time.time > lastChangeTime + delay)
    36.         {
    37.             lastChangeTime = Time.time;
    38.  
    39.             // Set color of the last renderer to red
    40.             // and the color of the current one to green
    41.             renderers[greenIndex].material.color = Color.red;
    42.  
    43.             if (shouldChangeDirection)
    44.             {
    45.                 Array.Reverse(renderers);
    46.                 isReversed = !isReversed;
    47.             }
    48.  
    49.             greenIndex = (greenIndex + 1) % renderers.Length;
    50.             renderers[greenIndex].material.color = Color.green;
    51.         }
    52.     }
    53. }
    54.  
    But doing if (shouldChangeDirection) then there is no effect for the isReversed state i set from the other script.