• 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
Question by Patton7 · Jan 21, 2019 at 05:20 PM · variablerandomspawnrandom.rangespawner

Prefab script values do not update

Hello all! So basically I have this random generator generates I prefab that I have at random times and at random positions between a range I have determined. So the problem is, I have so that if my score variable gets to a certain number, I want to update two other variables. However, this code only seems to update it for only 1 next game object being generated, but it does not permanently change the stats.

Here is the code:

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

 public class SpawnerScript : killEnemy {
 public GameObject enemy;
 public Vector2 spawnValues;
 public float spawnWait;
 public int startWait;
 public float spawnMostWait;
 public float spawnLeastWait;
 public bool terminate;

 // Use this for initialization
 void Start () {
     StartCoroutine(Spawner());
 }
 
 // Update is called once per frame


 public bool leveled;

 void Update()
 {
     int[] levelNums = { 5, 10, 20, 30, 40, 50 };
     for (int i = 0; i < 6; i++)
     {
         if (score == levelNums[i] && !leveled)
         { 
                 spawnMostWait += 1;
                 spawnLeastWait += 1;
                 leveled = true;
            
         }
     }
     spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
     
     

 }

 void CollectSavedValues()
 {

     score = PlayerPrefs.GetInt("exampleIntSave");

 }
 void CollectSavedValuesDead()
 {

     dead = PlayerPrefs.GetInt("exampleIntSave");

 }

 IEnumerator Spawner()
 {
     yield return new WaitForSeconds(startWait);
     while (!terminate)
     {
         Vector2 spawnPosition = new Vector2(Random.Range(spawnValues.x-8, spawnValues.x), Random.Range(-spawnValues.y, spawnValues.y));


         Instantiate(enemy, spawnPosition + Vector2.zero, gameObject.transform.rotation);
         yield return new WaitForSeconds(spawnWait);
         
         if(dead == 1)
         {
             terminate = true;
         }
     }
 }
 }
Comment

People who like this

0 Show 3
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 KISP · Jan 21, 2019 at 07:20 PM 1
Share

You should seriously reconsider how you've set this up. If you use this logic in the Update method, you're going to level up only once unless you remove the leveled variable. But if you take out that variable, your stats are going to become very high very quickly. The only time you need to check whether to increase the stats is when the score changes, so you should move your stat increase logic into whatever function increases the score. Likewise, rather than changing spawnWait every frame, change it instead in the Spawner function right before/after you use it.

Making these changes may not solve your problem, but it will make your script much easier to debug in the future.

avatar image Patton7 KISP · Jan 21, 2019 at 08:19 PM 0
Share

Yes! Thanks so much! it worked.

avatar image KISP Patton7 · Jan 22, 2019 at 02:35 PM 0
Share

Cool, I'll repost it as an answer then.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Legend_Bacon · Jan 21, 2019 at 05:51 PM

Hello there,


If by "Stats" you mean "spawnLeastWait" and "spawnMostWait", then you do have a logic error in there.

The bool "leveled" needs to be false to increase those variables by 1, however when that happens you immediately set it to true and never to false again.


That means that the stats will be at most increased ONCE, and then never again since "leveled" is never set to false.


I hope that helps!

Cheers,

~LegendBacon

Comment

People who like this

0 Show 1 · 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 Patton7 · Jan 21, 2019 at 06:26 PM 0
Share

Yeah so the issue with that is that when i do that, because it's the update method, it increases way too much since it checks once per frame, which is why I added that variable.

avatar image

Answer by KISP · Jan 22, 2019 at 02:35 PM

You should seriously reconsider how you've set this up. If you use this logic in the Update method, you're going to level up only once unless you remove the leveled variable. But if you take out that variable, your stats are going to become very high very quickly. The only time you need to check whether to increase the stats is when the score changes, so you should move your stat increase logic into whatever function increases the score. Likewise, rather than changing spawnWait every frame, change it instead in the Spawner function right before/after you use it.

Making these changes may not solve your problem, but it will make your script much easier to debug in the future.

Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

112 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 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

How To Random Range a Float? 2 Answers

Using Vector3 in ViewportToWOrldPoint 1 Answer

Random Spawn, Random Prefab 2 Answers

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

random place generator 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