Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Aug 18, 2016 at 07:33 PM by wasicool7.
avatar image
0
Question by wasicool7 · Aug 11, 2016 at 11:16 PM · spawnscorefaster

Unity2D: How to make spawn object gradually go faster after the player collects 10 points?

Hi, so I was wondering if there was a way to make a spawned object gradually moves/go faster after the player (you the user) collects 10 points. And faster when the player collects another 10 points and so on and so on?

This is my movement script attach to my objects that get spawned in:

 public class Movement : MonoBehaviour
   {
    public static int movespeed = 20;
    public Vector3 userDirection = Vector3.right;

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

This is my score script attach to my player

 public int Score;
 public Text ScoreText;

 void Start ()
 {
     Score = 0;
     SetScoreText ();
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("Pick Up")) 
     {
         other.gameObject.SetActive (false);
         Score = Score + 1;
         SetScoreText ();
     }
 }
 
 void SetScoreText ()
 {
     ScoreText.text = "Score: " + Score.ToString ();
 } 

And this is my generateEnemy script:

 public GameOverManager gameOverManager = null;

 [HideInInspector]
 public float minBlobSpawnTime = 2;
 [HideInInspector]
 public float maxBlobSpawnTime = 5;
 [HideInInspector]
 public bool generateBlobs = true;
 [HideInInspector]
 public GameObject blobPrefab = null;
 [HideInInspector]
 public GameObject blobRoot = null;
 [HideInInspector]
 public float minimumYPosition = -0.425f;
 [HideInInspector]
 public float maximumYPosition = 0.35f;

 [HideInInspector]
 public float minDaggerSpawnTime = 2;
 [HideInInspector]
 public float maxDaggerSpawnTime = 5;
 [HideInInspector]
 public bool generateDaggers = true;
 [HideInInspector]
 public GameObject daggerPrefab = null;
 [HideInInspector]
 public GameObject daggerRoot;
 [HideInInspector]
 public float minimumXPosition = -11.5f;
 [HideInInspector]
 public float maximumXPosition = 11.5f;

 public Camera camera = null;

 // Use this for initialization
 void Start ()
 {
     generateBlobs = ((generateBlobs) && (blobPrefab != null) && (blobRoot != null));
     generateDaggers = ((generateDaggers) && (daggerPrefab != null) && (daggerRoot != null));

     if (camera == null)
     {
         Debug.LogError("GenerateEnemy: camera is not set in the inspector. Please set and try again.");
         camera = Camera.main;
     }

     if (gameOverManager == null)
     {
         Debug.LogError("GenerateEnemy: gameOverManager not set in the inspector. Please set and try again.");
     }

     if (generateBlobs)
     {
         StartCoroutine(GenerateRandomEnemy(true, blobPrefab, blobRoot, minBlobSpawnTime, maxBlobSpawnTime));
     }

     if (generateDaggers)
     {
         StartCoroutine(GenerateRandomEnemy(false, daggerPrefab, daggerRoot, minDaggerSpawnTime, maxDaggerSpawnTime));
     }
 }
 
 // Update is called once per frame
 void Update ()
 {
     DestroyOffScreenEnemies();
 }
 
 // Spawn an enemy
 IEnumerator GenerateRandomEnemy(bool generateOnYAxis, GameObject prefab, GameObject root, float minSpawnTime, float maxSpawnTime)
 {
     if ((prefab != null) && (gameOverManager != null))
     {
         if (!gameOverManager.GameIsPaused())
         {
             GameObject newEnemy = (GameObject) Instantiate(prefab);
             newEnemy.transform.SetParent(root.transform, true);

             // set this in the prefab instead
             // newEnemy.transform.position = new Vector3(newEnemy.transform.parent.position.x, 0.5f, newEnemy.transform.parent.position.z);

             // or if you want the y position to be random you need to do something like this
             if (generateOnYAxis)
             {
                 newEnemy.transform.position = new Vector3(newEnemy.transform.parent.position.x, Random.Range(minimumYPosition, maximumYPosition), newEnemy.transform.parent.position.z);
             }
             else
             {
                 newEnemy.transform.position = new Vector3(Random.Range(minimumXPosition, maximumXPosition), newEnemy.transform.parent.position.y, newEnemy.transform.parent.position.z);
             }
         }
     }
     yield return new WaitForSeconds(Random.Range(minSpawnTime, maxSpawnTime));

     StartCoroutine(GenerateRandomEnemy(generateOnYAxis, prefab, root, minSpawnTime, maxSpawnTime));
 }
 
 public void DestroyOffScreenEnemies()
 {
     GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
     if ((enemies.Length > 0) && (camera != null))
     {
         for  (int i = (enemies.Length - 1); i >= 0; i--)
         {
             // just a precaution
             if ((enemies[i] != null) && ((camera.WorldToViewportPoint(enemies[i].transform.position).x > 1.03f) || 
                                          (enemies[i].transform.position.y < -6f)))
             {
                 Destroy(enemies[i]);
             }
         }
     }
 }

}

(This script has an generateEnemyCustomEditor script that references it, to show in the inspector)

Thank you :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by flatalex123 · Aug 12, 2016 at 08:46 AM

This code should now works. Please mark as answer if it works for you as well, if not tell me want is wrong and i will try to help

 public Text ScoreText;
 public AudioClip Coinsound;
 public Text Highscoretext;
 public GameObject theBadGuy;
 public 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;
         if (Score == 10) {
             //change "BadGuy" to the name of your enemy
             theBadGuy = GameObject.Find ("BadGuy");
             movement = theBadGuy.GetComponent<Movement> ();
             movement.movespeed += 10;
         }
         SetScoreText ();
         AudioSource.PlayClipAtPoint (Coinsound, transform.position);
 
     }
 }
 
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flatalex123 · Aug 12, 2016 at 09:00 AM 0
Share

repeat the code for 20, 30, 40 etc

avatar image wasicool7 flatalex123 · Aug 12, 2016 at 03:18 PM 0
Share

Yeah, sorry. I got this error: error CS0029: Cannot implicitly convert type int' to bool' and this error: error CS0031: Constant value 10' cannot be converted to a int'

avatar image flatalex123 · Aug 13, 2016 at 03:01 AM 0
Share

Could you please post the code I give, so I can check something

avatar image flatalex123 · Aug 13, 2016 at 03:04 AM 0
Share

Also change

Score = 10 to score == 10

avatar image wasicool7 flatalex123 · Aug 13, 2016 at 08:31 PM 0
Share

Thank you for your response. I also got two other error which I didn't mention before because I thought I can sort it out but I couldn't! :( .So these are the errors:

error CS0029: Cannot implicitly convert type $$anonymous$$ovement' to UnityEngine.GameObject'

And

error CS0031: Constant value 10' cannot be converted to a int'

But this is my code so far:

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

 private int Score;
 public int highScore = 0;

 
 void Start () 
 {
     Score = 0;
     SetScoreText ();
     if (PlayerPrefs.Has$$anonymous$$ey ("Highscore")) 
     {
         highScore = PlayerPrefs.GetInt("Highscore");
     }
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("Pick Up")) {
         other.gameObject.SetActive (false);
         Score = Score + 1;
         if(Score == 10){
             GameObject thePlayer = GameObject.Find("Player");
             enemy = thePlayer.GetComponent<$$anonymous$$ovement>();
             $$anonymous$$ovement.movespeed+= 10.0f;
         }
         SetScoreText ();
         AudioSource.PlayClipAtPoint (Coinsound, transform.position);

     }
 }

Once again thank you for the reply. :)

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

spawn object every 10 score points 1 Answer

How to identify players on Photon Unity? 2 Answers

Bullet spawning at high Player speed 1 Answer

NetworkServer is not active. Cannot spawn objects without an active server. 0 Answers

Spawn Random Object script spawns too much 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges