0
\$\begingroup\$

In unity game I've put two scripts in single object i want to these scripts are enable and disable on random time so how can do?

\$\endgroup\$
  • \$\begingroup\$ Do you want them to alternate, so one turns off and the other turns on at the same moment, or should each one just toggle randomly, independent of what the other is doing? Do you have any constraints on how long they should stay on or off for? \$\endgroup\$ – DMGregory Nov 10 '16 at 14:54
  • \$\begingroup\$ no nothing put any kind of constraints I want to just enable and disable scripts on random time. \$\endgroup\$ – Chetan Nov 11 '16 at 6:20
1
\$\begingroup\$

First, create empty object .
Then, attach this script to the object (and don't forget to modify it).

The following code is untested:

/* 
the how-to-use is in multiple comment 
To stop the script from running , set active false from outside the object using script 
UNTESTED
*/

using UnityEngine;
using System.Collections;

public class /* script name */ : MonoBehaviour {
  public float RandomizedTime;
  public int ActiveOrDeactive;

  void StartTheRandom(int TheObjectId,GameObject TheObject,string HelloAf) {
    GameObject AttachObject = TheObject;
    /* Set The Random Time in second , 0 Is minimal random time , 10 is the max */
    RandomizedTime = Random.Range(0f,10f);
    /* 1 Is Active , 2 Is De-active */
    ActiveOrDeactive = Random.Range(1,2);
    StartCoroutine(startWaiting(RandomizedTime,TheObject,ActiveOrDeactive,HelloAf,TheObjectId));
  }

  IEnumerator startWaiting(float theTime,GameObject GameGameObject,int ActiveOrNot,string HelloAfff,int ObjectId) {
    yield return new WaitForSeconds(theTime);
    if( ActiveOrNot == 1 ) {
      GameGameObject.GetComponent<HelloAfff>().enabled = true;
    }
    if ( ActiveOrNot == 2 ) {
      GameGameObject.GetComponent<HelloAfff>().enabled = false;
    }
    StartTheRandom(ObjectId,GameGameObject,HelloAfff);
  }

  // Use this for initialization
  void Start () {
    /* change firstobjectname and secondobjectname to the object that have the script */
    GameObject gameobjectone = GameObject.Find("firstobjectname");
    GameObject gameobjecttwo = GameObject.Find("secondobjectname");
    /* change onehere and twohere with the name of the script in the object */
    public string FirstScriptName = "onehere";
    public string SecondScriptName = "twohere";
    StartTheRandom(1,gameobjectone,FirstScriptName);
    StartTheRandom(2,gameobjecttwo,SecondScriptName);
  }
}

I hope this works :)

\$\endgroup\$
1
\$\begingroup\$

Just call static function on any script you want to have this behavior.

Here is the tested code:

using UnityEngine;
using System.Collections;

public class SwitchScriptsAtRandom : MonoBehaviour {

public MonoBehaviour script;
public float minTime = 1f;
public float maxTime = 4f;


// Use this for initialization
void Start () {
    StartCoroutine(ConstantlySwitchScrtiptsState(minTime, maxTime, script));
}

/// <summary>
/// Enables or Disables scripts depending on it's current state. Refreshes by random time.
/// Don't turn off this the script itself if you don't want the switching to interrupt.
/// You won't be able to enable it with itself.
/// </summary>
/// <param name="minWaitTime">Mininal random amount of time to wait until script changes it's state</param>
/// <param name="maxWaitTime">Maximal random amount of time to wait until script changes it's state</param>
/// <param name="scriptToSwitch">Reference to instance of the script</param>
/// <returns>WaitForSeconds</returns>
public static IEnumerator ConstantlySwitchScrtiptsState(float minWaitTime, float maxWaitTime, MonoBehaviour scriptToSwitch)
{
    while (true)
    {
        /*
         * If you want to check if gameobject is enabled you can use - scriptToSwitch.gameObject.activeSelf; 
         * Also you could check by scriptToSwitch.isActiveAndEnabled;
         * https://docs.unity3d.com/ScriptReference/Behaviour-isActiveAndEnabled.html
         */
        scriptToSwitch.enabled = !scriptToSwitch.enabled;

        // Make sure that values are correct. You can check with "if" and "throw" an exception if you have to be sure that values the developer enter are definitely correct.
        minWaitTime = Mathf.Clamp(minWaitTime, 0, maxWaitTime);
        maxWaitTime = Mathf.Clamp(maxWaitTime, 0, maxWaitTime);

        yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime));
    }
}

}
\$\endgroup\$
0
\$\begingroup\$

Your gameobject Go continast ScriptA and ScriptB. So make another script OnOffController and attached to your gameobject GO

ScriptA objScriptA 
ScriptB objScriptB
public float minRandomTimeInSeconds = 10;
public float maxRandomTimeInSeconds = 10;
void Start(){
  objScriptA = gameObject<objScriptA>(); 
  objScriptB = gameObject<objScriptB>(); 
  StartCoroutine(OnOffRandomly(true));
  StartCoroutine(OnOffRandomly(false));
}

void OnOffRandomly(bool state){
    while(true){
    yield return new WaitForSeconds(Random.Range[minRandomTimeInSeconds,maxRandomTimeInSeconds]);
    objScriptA.enabled = state;
    objScriptA.enabled = state;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.