help: background scrollspeed once certain score is reached

The following is code from two separate classes I learned during space shooter tutorial, but I want more.
I want to set up the scroll speed of the back ground to almost look like warp style once the player reaches a certain score. I will post the code from both of my classes involved. With the current code, nothing happens once I have reach Score of 50 which takes only 6 hits of an asteroid.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {
    public GameObject[] hazards;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;
    public GUIText scoreText;
    public int score;
    public GUIText restartText;
    public GUIText gameOverText;
    private bool gameOver;
    private bool restart;


    // Use this for initialization
    void Start () {
       StartCoroutine(SpawnWaves ());
        score = 0;
        UpdateScore();
        gameOver = false;
        restart = false;
        restartText.text = "";
        gameOverText.text = "";
    }
   
   
    // Update is called once per frame
    void Update () {
        if (restart)
        {
            if (Input.GetKeyDown (KeyCode.R))
            {
                SceneManager.LoadScene("Space Shooter Scene");
            }
        }
      
    }

    IEnumerator SpawnWaves ()
    {
        yield return new WaitForSeconds(startWait);
        while (true)
        {
            for (int i = 0; i < hazardCount; i++)
            {
                GameObject hazard = hazards[Random.Range(0, hazards.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(hazard, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(spawnWait);
            }
            yield return new WaitForSeconds(waveWait);

            if (gameOver)
            {
                restartText.text = "Press 'R' for game restart";
                restart = true;
                break;
            }
        }
    }

    void UpdateScore ()
    {
        scoreText.text = "Score: " + score;
    }

    public void AddScore (int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    public void GameOver ()
    {
        gameOverText.text = "Game Over";
        gameOver = true;
    }



}



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BG_Scroller : MonoBehaviour {

    public float scrollSpeed;
    public float tileSizeZ;
    private Vector3 startPosition;
    private GameController gameController;

    // Use this for initialization
    void Start () {
        startPosition = transform.position;
        gameController = gameController.GetComponent<GameController>();
    }
   
    // Update is called once per frame
    void Update () {
        float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ );
        transform.position = startPosition + Vector3.forward * newPosition;

        if (gameController.scoreText.text == "Score: 50")
        {
            scrollSpeed = scrollSpeed * 50;
        }
    }
}

Just a few things here…

  1. use code tags, it will display your code that you’ve posted in a format much easier for people here to read. Instructions on how are here: Using code tags properly - Unity Engine - Unity Discussions

  2. You’re checking the score in an oddball way. Don’t check the score text. Check the score variable (simply called “score” - no quotes).

  3. You’re currently checking if the score is exactly 50 (or attempting to check that). Use >= to check if it’s greater than or equal to the score you’re checking.

  4. less important, but I’d do this check as the first part of your update, but it’s no big deal how you have it now… no one will notice the one frame you missed setting the speed for :slight_smile:

The modified update of BG_Scroller:

// Update is called once per frame
void Update () {
   if (gameController.score >= 50)
   {
      scrollSpeed = scrollSpeed * 50;
   }
   float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ );
   transform.position = startPosition + Vector3.forward * newPosition;
}

I have tried the score variable too, with no luck.

also this is the error I keep seeing

Object reference not set to an instance of an object

OK, I’ve posted the code above, but the error you’re seeing needs more details. You need to send us the full error. It will have a line number indicating what object variable you have not properly “linked” to something (that’s what that error means).

For example, If it’s the error being reported is line 10 of BG_Scroller, then what you do is click on the object in the hierarchy that has the BG_Scroller script attached to it. Next you drag in the GameController script onto the spot in the property inspector for “gameController”. This associates this variable with that script.

rgr, i will provide the code and two screen shots showing my inspectors.

NullReferenceException: Object reference not set to an instance of an object
BG_Scroller.Update () (at Assets/Scripts/BG_Scroller.cs:22)

So it’s telling you that the error is on line 22 of your BGScroller script. So look at line 22 and see what’s being called. You’ve updated your script since you posted it initially, so I’m not sure what you have there now. Post your updated BGSCroller script if you can’t figure it out.

I think the problem is your BGScroller isn’t finding the GameController script.

In BGScroller, I think you might be best to change this:

private GameController gameController;

to this:

public GameController gameController;

Then drag that script onto your BGScroller script’s gameController slot in the inspector.

And then remove this from the Start function in BGScroller:

gameController = gameController.GetComponent<GameController>();

This would be your updated BGScroller.cs, I believe:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BG_Scroller : MonoBehaviour {

    public float scrollSpeed;
    public float tileSizeZ;
    private Vector3 startPosition;
    public GameController gameController;

    // Use this for initialization
    void Start () {
        startPosition = transform.position;      
    }
 
    // Update is called once per frame
    void Update () {
        if (gameController.score >= 50)
        {
            scrollSpeed = scrollSpeed * 50;
        }
        float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ );
        transform.position = startPosition + Vector3.forward * newPosition;       
    }
}

Don’t forget to link your GameController script via the inspector!

1 Like

I think you’re right :slight_smile:

1 Like

The private to public change while dragging the game controller worked! Thanks. Now comes my next problem lol, it shoots the speed so fast the mathf.repeat doesn’t seem to be able to keep up, and i lose my background lol. I was thinking of slowing the speed value to maybe 25 but just incase do you have any suggestions on how to adjust the script instead.

thank you for the all the help btw, I spent 4 years learning coding in college but not a single class worked with game engines, it only worked with IDE’s and flash

All good… Programming is still the same, in principle :wink: Learning the Engine is a bit more, for sure - part of the package.

As for your speed, I’d try different speeds. Repeat should loop/wrap the value…Maybe just adjust it in the inspector while the game is running & see what looks good? :slight_smile:

1 Like

will do, i figured maybe 50 is a bit extreme lol.

EDIT: actually on that note, it looks like my scroll speed just said infinity now but 50 still worked ok, ill try and figure out why

The New error that popped up is this.

transform.position assign attempt for ‘Background’ is not valid. Input position is { NaN, NaN, NaN }.
UnityEngine.Transform:set_position(Vector3)
BG_Scroller:Update() (at Assets/Scripts/BG_Scroller.cs:21)

Ok maybe the problem is you keep multiplying it.
like, whenever you’re at the stage to increase it , you keep multiplying it by 50 (based on some code above)

So, maybe you just need :
(just an idea)

 if(!beenset && score >= 50) {
   beenset = true;
   speed = 50;
  }

just set it once :slight_smile:

ill give that a try

THAT WORKED

Cool :slight_smile: