Method after some time

Hello guys, I’m stuck on new problem, I would like to make one method after some time, see code below:

using UnityEngine;

public class Spawn : MonoBehaviour
{
    public GameObject[] Obstacles;
    public float spawnMin = 1f;
    public float spawnMax = 2f;
    float x = 0;
    float b = 0;
    float timer;

    private Vector3 MovingDirection = Vector3.up;
    public float speed = 5;

    void Start()

    {
        Make();
    }

    void Make()
    {
        x = Random.Range(1, 4);
        b = Random.Range(3, 5);

        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, x, 1), Quaternion.identity);
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, x-b, 1), Quaternion.identity);       
        Invoke("Make", Random.Range(spawnMin, spawnMax));
    }

    void Make2()
    {
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, 1, 1), Quaternion.identity);
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, 1, 1), Quaternion.identity);
        Invoke("Make2", Random.Range(spawnMin, spawnMax));
    }

}

I would like to start “Make2” after some time(for example after 10sec)… I tryed to use other script to made timer using “Time.deltaTime” but it not works or i dont understand how to use variable from other script… ( i tryed to use GetComponent)… Please, give me advice how can I make my script? And when Make2 start it must stop Invoke in “Make”.

Thank you all, who will answer me and sorry for my English:)

try this

using UnityEngine;
 
public class Spawn : MonoBehaviour
{
    public GameObject[] Obstacles;
    public float spawnMin = 1f;
    public float spawnMax = 2f;
    float x = 0;
    float b = 0;
    float timer;
    bool invokeOK;
 
    private Vector3 MovingDirection = Vector3.up;
    public float speed = 5;
 
    void Start()
 
    {
        waitFor(10.0.f); // start everything  with a 10 sec timer
    }
 
    void Make()
    {
        x = Random.Range(1, 4);
        b = Random.Range(3, 5);
 
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, x, 1), Quaternion.identity);
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, x-b, 1), Quaternion.identity);  
        if(invokeOK)
        {    
            Invoke("Make", Random.Range(spawnMin, spawnMax));
        }
    }
 
    void Make2()
    {
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, 1, 1), Quaternion.identity);
        Instantiate(Obstacles[Random.Range(0, Obstacles.GetLength(0))], new Vector3(1, 1, 1), Quaternion.identity);
        Invoke("Make2", Random.Range(spawnMin, spawnMax));
    }

    IEnumerator waitFor(float time)
    {
        Make(); // start Make
        yield return new WaitForSeconds(time); // wait for x time
        invokeOK = false;
        Make2(); // start make2
    }
 
}

You’re already using ‘Invoke’ … with a time delay… I’m not sure what the hold up is on doing it with a fixed time of 10 seconds?
I was just about to suggest a bool to stop/not restart the Make, but as I was writing, someone else gave a comprehensive response :slight_smile:

1 Like

I was trying to use this, but with this code it does not spawn anything at all… It’s like that methods Make or Make2 don’t runs… I appended only “using System.Collections” and it seems like all code is ok, but something went wrong…

I try to make that on start of the game the Make method starts, but after some other time I need to stop running Method “Make” and start method “Make2” and again after some time run “Make” and again and again. I need to make something like this, it will be like after some time game will be harder/easier.

The Invoke method, I use to spawn some objects at specific time and interval. And I need to make another intensivity of spawn this object for time interval and then it must become as was at start of game.

I’m sorry for the fact that I can’t express myself very clearly, because my native language is not English:)

Change this (from the other poster):

 waitFor(10.0.f);

to: StartCoroutine(waitFor(10));

1 Like

And one more question, can u advise me what i doing wrong or maybe i do impossible with this?

I made 2 scripts. One is

using UnityEngine;

public class timer : MonoBehaviour {

    public float time;

    void Update()
    {
        time += Time.deltaTime;
    }
}

and other:

using UnityEngine;

public class Spawn : MonoBehaviour
{
float time = 0;
void Update()
    {
        time = GetComponent<timer>().time;
        print("timer" + time);
    }
}

It’s like a test, I think that I can do my “Make” or “Make2” according to the score variable from other script. I do same like in this 2 scripts, but in unity i see “NullReferenceException: Object reference not set to an instance of an object”. How can I get variable from another script and use it in “if” in another one ?
:

Well, it would be nice to know if the update I posted fixed your script lol… before we move onto the next question.

If it’s null , then I presume that means that you don’t have ‘timer’ and ‘Spawn’ on the same game object … :slight_smile:

1 Like

I tested it now. It’s ok and work good, I will try to realize my idea with it. But, I have one more question that don’t give me rest:smile: I tried to made this script before I decide to look for another way, but it was not worked as I wrote above.

But, is there another way to use variable from another script without attaching it?

You have to be able to get the value somehow. You can do this by…
A) getting a reference to any piece of the other object that has the script. (and looking it up that way)
B) being on the same object and getting the script (of course)
C) you can fetch a static variable from another script
D) the other script can have a reference to your script, and the other script can update your variable/call a method that updates your variable, etc…

Thank you very much for your answers and advices, they are help me too much!
To declare static variable I must only write like " public static int b = 0 " and then I can access it from another script?
I now tried static variable, to access it from another script i just set new variable for example “k” and write " k = scriptname.b" ? Is it correct?

Yep. You’d access it like: ScriptName.b

Static variables can be really good, but you shouldn’t use them if your only reason is afraid of finding a reference. I just thought I’d say that, in case that was your reason. Anyways, as you go, you’ll find what works well for you.
So you’re welcome & have fun :slight_smile:

Yes, You’r right. Honestly saying I don’t know how I can do it correctly.

Well, you’ll learn :slight_smile: practice… try the learn section on the website (Unity’s). Try a tutorial or 2 (or three…) etc

If you got most of your stuff working and you want to try something new & the static variable will help you try it out, I mean… just use it… :slight_smile:

I have to go for now. Take it easy. Enjoy your game making

Thank you, now I’m just try to learn how to make reference from one script to another variable in another script… But can’t find simple explanation and example :frowning:

sorry about the typo in my first response. it sounds like you got it working now.
referencing values from an other script is easy. there are few whys to do it.
You typically do it via GetComponent<>. so I would do some searches on that.

Thank you for your answer and advice :slight_smile:
Yes, I try to do it via GetComponent but it works only if this 2 scripts attached to same object :frowning: But I would like to do it from different objects.

lets say you have 2 objects, cube1 and cube2.
cube1 has script1 and cube2 has script2

script2 has

using UnityEngine;
public class script2 : MonoBehaviour
{
    public int number;
   
    void Start()
    {
        number = Random.Range(-10.0f, 10.0f);
    }

}

script1 will find the gameobject and the script attached to the gameobject.

using UnityEngine;
public class script1 : MonoBehaviour
{
    private script2 S2;
    void Start()
    {
        S2 = GameObject.Find("Cube2")<script2>();  // find and store cube2 script as S2
        // now we can get the public values from script2
        Debug.Log(S2.number);
    }

}

Adding another example for cube1 with script1 and cube2 with script2.
You can do this:

// Script1
public Script2 s2; // you can drag cube2 to here and it will automatically reference the 'script2' on it.
public GameObject cube2;  // you can also drag cube2 here and reference the game object (inspector)
// later in the script, maybe in Start() for example (could be anywhere else, too)
void Start() {
  s2.myInt = 5; // if 'myInt' is public on script2, we've just assigned 5 to it.
  // and/or
 float f =  cube2.GetComponent<Script2>().myFloat; // we read the 'myFloat' variable in Script2 on cube2
 }

Anyways, just some examples. You’ll get used to it :slight_smile:

These are things you learn in the beginning. Don’t hesitate to use some resources found here: Learn