How can I apply the changes in runtime ?

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

public class AnimFlyertransportationController : MonoBehaviour
{
   public GameObject JetFlame;
   public bool turnOffOn = false;

   // Start is called before the first frame update
   void Start()
   {
       TurnAllAnimtorsOff(transform, turnOffOn);

       if (turnOffOn)
       {
           JetFlame.SetActive(false);
       }
       else
       {
           JetFlame.SetActive(true);
       }
   }

   // Update is called once per frame
   void Update()
   {

   }

   private void TurnAllAnimtorsOff(Transform root, bool onOff)
   {
       Animator[] animators = root.GetComponentsInChildren<Animator>();

       foreach (Animator a in animators)
       {
           if (onOff)
           {
               a.enabled = false;
           }
           else
           {
               a.enabled = true;
           }
       }
   }
}

I need that if in runtime I change the turnOffOn flag state apply the changes in real time in the Update for all Animators and also for the JetFlame. Now it’s only apply the changes in the editor before running the game.

Because you do your check only in start, which is one time called before the first frame.

Put the code into the Update method, that is called each frame.

1 Like

but then it will do the code in the TurnAllAnimtorsOff function all the time nonstop.

It shouldn´t be that big problem, as long as you don´t change the values. If you only wanna do it if something has changed, you need your own events which are triggered, as soon as a value has changed. So as long as it works i would put it into the Update method

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimFlyertransportationController : MonoBehaviour
{
   public GameObject JetFlame;
   public bool turnOffOn = false;

   private bool currentTurnOffOn;
   // Start is called before the first frame update
   void Start()
   {
       //if you change turnOffOn in editor make sure UpdateTurnOnAndOff() is run once on start()
       currentTurnOffOn = !turnOffOn;
       UpdateTurnOnAndOff();
   }
   // Update is called once per frame
   void Update()
   {
       UpdateTurnOnAndOff();
   }

   private void UpdateTurnOnAndOff()
   {  //if turnOffOn hasn't changed dont do anything below return
       if(currentTurnOffOn == turnOffOn)
          return;
     
       TurnAllAnimtorsOff(transform, turnOffOn);
       if (turnOffOn)
       {
           JetFlame.SetActive(false);
       }
       else
       {
           JetFlame.SetActive(true);
       }

       currentTurnOffOn = turnOffOn;

       debug.Log("did I show up every frame on the console?");
   }

   private void TurnAllAnimtorsOff(Transform root, bool onOff)
   {
       Animator[] animators = root.GetComponentsInChildren<Animator>();
       foreach (Animator a in animators)
       {
           if (onOff)
           {
               a.enabled = false;
           }
           else
           {
               a.enabled = true;
           }
       }
   }
}
1 Like