- Home /
Changing one script through another?
Hello!
Im currently making my first ever video game based of the tutorial made by Brackeys on youtube. I've made a little own "spin" on it but Im currently a little stuck. I've so far been able to make the game work so blocks are spawning one at a time in fixed spawnpoints with a certain speed moving towards the "player". I've also managed to in the script for the obstacles moving add speed to them whenever the in-game time exceeds certain "checkpoints". Now I want to make it so that in the script for the obstacles moving where i've programmed so they move faster that also at the same time the interval between spawning the blocks increase aswell. My problem here is that the programing for the blocks Spawning time is in another script. I've tried googling a little bit but have not found anything useful so far. I hope this makes sense and I will add the two scripts down below.
Thanks in advance! (EDIT - forgot to mention that i use c#)
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject BlockPrefab;
public float TimeBetweenSpawns = 1f;
private float TimeToSpawn = 2f;
void Update()
{
if (Time.time >= TimeToSpawn)
{
SpawnBlocks();
TimeToSpawn = Time.time + TimeBetweenSpawns;
}
}
void SpawnBlocks ()
{
int randomindex = Random.Range(0, SpawnPoints.Length);
for (int i = 0; i < SpawnPoints.Length; i++)
{
if (randomindex == i)
{
Instantiate(BlockPrefab, SpawnPoints[i].position, Quaternion.identity);
}
}
}
}
(Break for easier viewing)
using UnityEngine;
public class ObstacleMovement : MonoBehaviour {
public Rigidbody rb;
public float ForwardForce = -2000f;
void FixedUpdate ()
{
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
if (Time.time > 5)
{
ForwardForce = -6000f;
}
if (Time.time > 10)
{
ForwardForce = -7000f;
}
if (Time.time > 20)
{
ForwardForce = -8000f;
}
}
}
Answer by madks13 · Jun 30, 2018 at 11:50 AM
When you instantiate your BlocPrefab, put it in an array. Either get the script as component from the block or from the array, or put only the scripts in the array : List blocks = new List();
var go = GameObject.Instantiate(blockPRefab);
blocks.Add(go);
var om = go.GetComponent<ObstacleMovement>();
//Do something with obstacle
or : List obstacles = new List();
obstacles.Add(GameObject.Instantiate(blockPrefab).GetComponent<ObstacleMovement));
//Do something with obstacles
Answer by Ermiq · Jun 30, 2018 at 02:06 PM
If BlockSpawner script is designed to have the only one instance at runtime (so it's not that every block uses its own BlockSpawner script instance but all blocks use the same instance of BlockSpawner) then you could make a static instance of the script to manage its variables from another classes:
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public static BlockSpawner Instance; //this gonna be yout access point
// to all variables in this class
public Transform[] SpawnPoints;
public GameObject BlockPrefab;
public float TimeBetweenSpawns = 1f;
public float TimeToSpawn = 2f;
void Awake ()
{
// Initialize it here (we make Instance variable to store this class instance)
Instance = this;
}
void Update()
{
if (Time.time >= TimeToSpawn)
{
SpawnBlocks();
TimeToSpawn = Time.time + TimeBetweenSpawns;
}
}
}
Now you can get access to BlockSpawner variables from another class by using
BlockSpawner.Instance._public_variable_in_blockSpawner_script
Like this:
using UnityEngine;
public class ObstacleMovement : MonoBehaviour
{
public Rigidbody rb;
public float ForwardForce = -2000f;
void FixedUpdate ()
{
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
if (Time.time > 5)
{
ForwardForce = -6000f;
BlockSpawner.Instance.TimeBetweenSpawns = 0.5f;
BlockSpawner.Instance.TimeToSpawn = 1f;
}
}
}
You also can make separate special class to store and manage all variables you could use. For example, I have a PlayerCharacter class and I manage all player's states there (such as bIsPlayerRunning, bIsPlayerShooting) and then I check these variables in other classes and do things like running animations and so on, and I can make PlayerCharacter.bIsPlayerShooting=false in the animations class when he's running and I don't need to do it in PlayerCharacter class.