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:)
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
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:)
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 ?
:
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.
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?
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
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
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
Yes, I try to do it via GetComponent but it works only if this 2 scripts attached to same object 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
These are things you learn in the beginning. Don’t hesitate to use some resources found here: Learn