1
\$\begingroup\$

I want to make my spawned objects (enemy prefabs) movement speed up every 10 points my player collects. This is my movement script, attached to my enemy prefab (so that it can be spawned in my game and move):

public static int movespeed = 20;
public Vector3 userDirection = Vector3.right;

public void Update()
{
    transform.Translate(userDirection * movespeed * Time.deltaTime); 
 }
}

And this is my score script attached to my player:

public Text ScoreText;
public AudioClip Coinsound;
public Text Highscoretext;
public GameObject enemy;
Movement movement;

private int Score;
public int highScore = 0;


void Start () 
{
    Score = 0;
    SetScoreText ();
    if (PlayerPrefs.HasKey ("Highscore")) 
    {
        highScore = PlayerPrefs.GetInt("Highscore");
    }
}
void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag ("Pick Up")) {
        other.gameObject.SetActive (false);
        Score = Score + 1;
        SetScoreText ();
        AudioSource.PlayClipAtPoint (Coinsound, transform.position);

    }
}

As mentioned before I want to make my spawned enemy prefabs to move faster when my player collects every ten points. Thank you :)

\$\endgroup\$
3
\$\begingroup\$

Since you speed is a static variable, you can adjust it from anywhere, and it will affect every entity using that speed. So, update your player script to adjust the speed when it updates the score.

void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag ("Pick Up")) {
        other.gameObject.SetActive (false);
        Score = Score + 1;
        SetScoreSpeedFactor(Score);
        SetScoreText ();
        AudioSource.PlayClipAtPoint (Coinsound, transform.position);

    }
}


SetScoreSpeedFactor(int score){
    int scoreFactor = (score/10);
    MovementScript.AdjustSpeed(scoreFactor);
}



public static int basemovespeed = 20;
private static int movespeed = 20;
public Vector3 userDirection = Vector3.right;

public static void AdjustSpeed(int adjustmentValue) {
    movespeed = basemovespeed + adjustmentValue;
}

public void Update()
{
    transform.Translate(userDirection * movespeed * Time.deltaTime); 
}

This will simply calculate the speed adjustment value as how many times 10 fits into the score. So, if your score is 20, the scoreFactor will equal 2. Therefore the result of calling AdjustSpeed will set your movespeed to 22.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your reply, I wanted to know do I make the SetScoreSpeedFactor a public float or a bool , etc. I got kinda confuse. Please and Thank you. :) \$\endgroup\$ – Jessca Stone Aug 19 '16 at 11:27
-2
\$\begingroup\$

If you were to have an empty object, then you enable/disable, thru script,it to increase the spawn rate as in when ever it becomes enabled, it is disabled and increases spawn rate, then when ever the score gets high enough, have it enable it. This is a very simple way to trade info between scripts instead having to get the varible from each script.

\$\endgroup\$
  • \$\begingroup\$ If you're able to access the script to enable or disable it, it's trivial to then modify the variable. Further, I think "hacks" like this make for very poor programming practices. \$\endgroup\$ – MichaelHouse Aug 19 '16 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.