Add input field to existing script. Sorry I’m a noob…

Hi I’m not a coder so please be gentle…

I found this code snippet on here and its does what I need, i.e enables a game object after a given time.

What I’d like to add is the ability to set the time via a field in the inspector that way I can attach it to several objects in my scene and have different timings for each.

Any help is appreciated.

using UnityEngine;
using System.Collections;

public class DelayedSetActive : MonoBehaviour

{
    void Start()
           

    {
        StartCoroutine(ActivateRoutine());
    }
  
    private IEnumerator ActivateRoutine()
 
    {
    
    yield return new  WaitForSeconds(4.0f);    // and now we wait !

        // make a list of all children
        Transform[] ChildrenTransforms = this.gameObject.GetComponentsInChildren<Transform>(true);
        foreach (Transform t in ChildrenTransforms)
            t.gameObject.SetActive(true);      // enable all the objects in the list (even the parent)

    }
}
using UnityEngine;
using System.Collections;

public class DelayedSetActive : MonoBehaviour

{

    public float delayValue; // expose class variable to show in the inspector, has to be public to appear

    void Start()
           

    {
        StartCoroutine(ActivateRoutine());
    }

    private IEnumerator ActivateRoutine()

    {
   
    yield return new  WaitForSeconds(delayValue);    // use the variable not a set value

        // make a list of all children
        Transform[] ChildrenTransforms = this.gameObject.GetComponentsInChildren<Transform>(true);
        foreach (Transform t in ChildrenTransforms)
            t.gameObject.SetActive(true);      // enable all the objects in the list (even the parent)

    }
}

next time it’s the thumbscrews :stuck_out_tongue: :smile:

Start can be a coroutine! And transform can be enumerated! :slight_smile:

public float DelayValue;

IEnumerator Start()
{
    yield return new WaitForSeconds(DelayValue);
    foreach (Transform t in transform)
    {
        if (t != transform)
            t.gameObject.SetActive(true);
    }
}
2 Likes

Awesome guys, thanks for the help!

@LeftyRighty : I can take the thumbscrews… its the cattle prod i don’t like :hushed:

1 Like