Hello! Total Unity/coding newbie here. I recently completed the 2D Space Shooter Tutorial and am trying to make it so the background and star field move faster when the player has won the game by getting a score of over 100. How would I go about doing that? I would assume under my background scroller script but the game controller script is what holds all the code for score. Any help would be greatly appreciated!
Hi @lfender95
Why not post the link to the tutorial in question “2D Space Shooter Tutorial” is so generic, there are other tutorials than Unity’s too. Don’t expect anyone to start googling for a tutorial for your question.
“How would I go about doing that?”
How was the effect done in that specific tutorial? It depends on that pretty much. Explain and break it down how it was done and someone most likely will be able to answer to your question.
FindObjectOfType<ObjectName>(); Use that to access other scripts. So you simply create a public method that returns your score within your score script, then call it in your scroller script.
if(score >= 100)
{
backGroundSpeed += backGroundSpeed * 2;
starSpeed += starSpeed * 4;
}
I don’t know what your variables look like so I made up backGroundSpeed and starSpeed but you should get the picture. All you’re really doing is increasing your speed variables based upon a condition.
@lfender95 I’m guessing that you are referring to the unity 3d space tutorial. I’m in the same situation. All I can understand is what has to happen but not what to type. Its just having to call the public void gameOver from the gamecontroller script to the background scroller, and just copy the previous scroller code with new variables. I know that is bad advice but it should work.
Oh, so all you’re trying to do is access the score variable?
type the name of the script GameController.score assuming you have static variables. If they are public and you have a Method() you can pass the information through to other scripts.
//Your game controller script "GameController"
public void scoreController(int newScore)
{
score += newScore;
if(score >= 100)
{
// increase your speeds here
}
}
//On your other script you call your function and pass the information
private int newScore;
// Let's create a reference to the script GameController
private GameController gc;
private void Awake()
{
// Let's initialize the gc variable
gc = GameObject.FindObjectOfType<GameController>();
}
void Update()
{
// Imagine there's code that updates newScore
// We need to pass this information to GameController script
gc.scoreController(newScore);
}