How to define a gameObject duration?

Hello,

I am new to Unity and I am trying to build a temporal bisection task. I have the following issues that I am struggling to resolve:

  1. In the practice block, consisted of 4 trials, I would like to present the stimulus-to-be-timed in two anchor durations, one per trial.
    2)In the experimental blocks (4 blocks-6 trials each), I want to present different stimulus with different/and random duration per trial.

Any ideas how I can implement the above?
Any help will be greatly appreciated!

First of all, it’s very bad practice to use jargon of any kind (biotechnical jargon of all things) other than that of the subject in which you’re asking about, especially without elaborating on its meaning.


This is a forum for programming help and game dev answers and you’re forcing anyone willing to help you to go lookup an entirely different subject just understand your question enough to help you with programming.


Onto the problem.

Using coroutines is what you’re looking for.

Things to know:

Start a coroutine with:

StartCoroutine("someName", onlyOneOptionalVariable); 

or

StartCoroutine(someName());

the former allows you to Stop the coroutine explicitly with

StopCoroutine("someName");

while the latter lets you add infinite variables, example,

StartCoroutine(someName(arg1,arg2,arg3));

using booleans is helpful or

StopAllCoroutines();

All coroutines start with “IEnumerator” followed by a name and look like

IEnumerator SomeName()
{
yield return null;
}

In any coroutine, if you use a while loop without “yield return null;” it will crash unity.

IEnumerator SomeName()
    {
     while(someRule)
    {
    //do stuff << this will crash
    }
    yield return null;
    }

IEnumerator SomeName()
    {
     while(someRule)
    {
    //do stuff << this will not
    yield return null;
    }
    }

This is how it answers your question

IEnumerator TrialCo()
        {
        //Start the trial
        tone.Play()
        yield return new WaitForSeconds(4);
        tone.Stop()
        yield return null;
        }

 IEnumerator RandomTrialCo()
        {
        //Start the trial
        tone.Play()
        int min = 0;
        int max = 5;
        var randSeconds = UnityEngine.Random.Range(min, max);
        yield return new WaitForSeconds(randSeconds);
        tone.Stop()
        yield return null;
        }

It’s worth noting that unity’s random range has a bug wherein if you ask for a random int between 0 and 1 it will always return 0, so just keep that in mind. You can get around it by asking for a int between 0 and 100 and multiplying by 0.01 and then rounding the result to int. Pretty annoying but w.e


I hope this helps, if you accept this answer please up vote it to help others facing the same issue find it easier.