Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by nicktheuma · May 10, 2014 at 05:01 PM · c#arrayspawninfinite runner

How to Spawn objects at a certain score.

I'm making an infinite runner and I'd like to be able to control what objects can be spawned when the player reaches a certain score. I'd like to start spawning the objects in the array (as shown in script) when the player score reaches 5. This is my script so far but it isin't working(It doesnt give me any errors either)

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : MonoBehaviour {
 
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
 
     float Score;
 
     HUDScript hud;
     
     void FixedUpdate ()
     {
         Spawn();
     }
 
 //
 //        hud = GameObject.Find("Main Camera").GetComponent<HUDScript>();
 //
 //        if(hud.playerScore == 5)
 //            gameObject.SetActive(false);
 
     
     void Spawn()
     {
         hud = GameObject.Find("Main Camera").GetComponent<HUDScript>();
 
         if(hud.playerScore == 5)
         {
             Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
             Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
         }
     }
 }
Comment
Add comment · Show 4
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 getyour411 · May 11, 2014 at 12:17 AM 0
Share

Any reason you are using FixedUpdate? You should move the hud = GameObject... out of Spawn if it needs to be called that ofen, put it in Start or make HudScript hud; public and drag/drop. The mix of calls to Spawn() within FixedUpdate and Invoke (which will loop multiple calls as well while playerScore = 5) looks problematic.

avatar image nicktheuma · May 11, 2014 at 09:23 AM 0
Share

I used fixed update so that the playerScore would be updated every frame..If I use start it will just be taken at the first frame and never again no? I need it to check the score constantly so that when it reaches a certain value I invoke spawn

avatar image Benproductions1 · May 11, 2014 at 09:36 AM 0
Share

Please see this video on why you should not be using FixedUpdate for this.

I don't see a reason why you would use Invoke in your spawn function to call itself...

When you say "it isn't working", I just find that a very descriptive question. Has anyone ever called you and said: "$$anonymous$$y computer isn't working, fix it for me?". Isn't that a load of useless information. What isn't working?

avatar image felixpk · May 11, 2014 at 11:50 AM 0
Share

Benproductions1, look at his $$anonymous$$arma, seems not to be the first Question he asked with a lot of useless information.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by felixpk · May 11, 2014 at 11:43 AM

     public GameObject[] obj;
     private float spawnMin = 1f;
     private float spawnMax = 2f;
     private HUDScript hud;
     private bool spawn;
 
     void Start() {
         hud = Camera.main.GetComponent<HUDScript>(); //you can get the reference directly from the camera
 
         StartCoroutine("SpawnRoutine"); //Starting a coroutine to be able to wait for seconds as you might not want to spawn every frame
     }
 
     IEnumerator SpawnRoutine() {
         while(spawn) {
             yield return new WaitForSeconds(5); //waits 5 secs to execute the code below
             Instantiate(obj[Random.Range(0, obj.Length-1)], transform.position, Quaternion.identity); // I think length-1 but not sure here
         }
     }
 
     void Update() {
         if(hud.playerScore >= 5) {
             spawn = true;
         }
     }

People really just can guess what your problem is but I tried to understand what you need and fixed your code. And I really don't know where these "objects" should spawn, or what spawnMin, spawnMax should be.

Make sure you:

  • use matching names for your variables

  • write precisely what your problem is

  • and explain precisely what your code should be doing!!

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How to Spawn one object at a time in Transform Array 1 Answer

Unity Engine issue... 1 Answer

Way Point Assignment 4 Answers

How Do I Add An Instantiated Object To An Array? 3 Answers

Spawning Objects Using An Array. 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