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 r_chevallier · Nov 25, 2014 at 09:25 AM · playerspawnintvarcoin

Spawn object every 5 coins collected

Hello,

I am trying to write a script to spawn a game object for every 5 coins collected.

I have the spawn working when I collect the first 5 coins. The problem is as long as coinsCollected = 5 then I get multiple spawns because start Spawn code is in Update. As soon as I collect another coin then coinsCollected does not = 5 so the spawning stops. How do I ad a variable or int for C# so the spawing stops immediately.

Also how would I make my script work so that every 5 coins collected a game object will spawn. The game object spawning is always going to be the same game object(like a power up).

Thanks in Advance.

Here is my code:

     public GameObject obj;
     public Vector3 spawnValues;
     private PlayerMove playerMove;
     private GUIManager gui;
     int myInt = 1;
 void Start ()
     {
         
         playerMove = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>();
         gui = FindObjectOfType(typeof(GUIManager)) as GUIManager ;
         transform.position = playerMove.transform.position;
     }
     
 void Update ()
     {
     if (gui.coinsCollected == 5){
         Spawn();
     }
 }

 void Spawn ()
 {
         Instantiate(obj, transform.position, Quaternion.Euler(Vector3.zero));
         //Instantiate
     }
 }    
Comment
Add comment · Show 7
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 r_chevallier · Nov 24, 2014 at 11:19 PM 0
Share

Hello, That doesn’t work because the coins collected are a score in itself. The spawned object per 5 coins collected is like a bonus. So I can’t keep resetting the coin collected score back to 0.

Your scripting did give me an idea though for a hidden secondary value attached to the coins that can be reset when 5 have been collected. What do you think? I welcome any feedback even obvious ones.

Thanks

avatar image GameVortex · Nov 25, 2014 at 09:35 AM 1
Share

The hidden value will work fine, just add 1 to the hidden score and the real score when collecting a coin. But I would recommend checking each time you add a new coin ins$$anonymous$$d of checking in each update. Then you have greater control and you could skip using the hidden value altogether by using the modulus operator to check if the value is divisible by 5:

Example:

 if(coinsCollected % 5 == 0)
 {
     Spawn();
 }
avatar image r_chevallier · Nov 25, 2014 at 04:11 PM 0
Share

I appreciate your help but when I tried your script my game object was spawning every time I collected a coin. Aren't all values divisible by 5 becuase it will be just broken down into a fraction? For example: 1/5 = 0.2

Does the modulus operator check for whole numbers only?

I did set up the hidden value scenario and it works but yours is simpler if it worked like I need it to.

Thanks

avatar image c-sprong · Nov 25, 2014 at 04:30 PM 1
Share

The modulus operator returns the remainder of the division, so for example 14%5 = 4 because 14 = 2*5 + 4. Testing if the remainder equals 0 is the same as testing if it is a multiple of the number you divide by.

It works fine on integers (whole numbers). coinsCollected should be an integer anyway.

avatar image c-sprong · Nov 25, 2014 at 05:26 PM 1
Share

That is not how the modulus operator works, though.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by c-sprong · Nov 25, 2014 at 09:48 AM

You could call Spawn() just after you increase coinsCollected, instead of in the Update function. That way it would only spawn one object every five coins. To make it spawn every five coins you can use a hidden variable that you reset after collecting 5, or do:

 if ((gui.coinsCollected % 5) == 0){ // Remainder of a division by 5 equal to 0 means it is true on multiples of 5
 }
 
Comment
Add comment · Show 2 · 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 r_chevallier · Nov 26, 2014 at 06:13 AM 0
Share

Thanks for your help. I will try your suggestions and let your know how it goes. I need to restructure my code a little so I don't have to use update.

avatar image f4bo · Nov 26, 2014 at 08:30 AM 0
Share

hey buddy, did you already checked my answer? that way you don't need to restructure anything and it works as you need to

avatar image
0

Answer by f4bo · Nov 25, 2014 at 08:39 PM

just change yourcode like this:

 int nextSpawn = 5;
 
 ...
 
 void Update ()
 {
   if (gui.coinsCollected == nextSpawn){
     Spawn();
     nextSpawn+=5;
   }
 }

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 c-sprong · Nov 26, 2014 at 10:37 AM 0
Share

That works, but I think events should really be handled as they occur, not polled for every frame.

avatar image c-sprong · Nov 26, 2014 at 11:03 AM 0
Share

Also, something like

 while (gui.coinsCollected>=nextSpawn){
     Spawn();
     nextSpawn+=5;
 }

would be more robust.

avatar image f4bo · Nov 26, 2014 at 06:17 PM 0
Share

my code works just as intended - it calls spawn every 5 coin, there really is no need to complicate things further. If you want to have it like an event - which make better sense - then you have to use the Ontrigger function not the Update and put that check there.

avatar image c-sprong · Nov 26, 2014 at 07:04 PM 0
Share

Your code may fail if the player can pick up multiple coins in the same frame, which is why I suggested an alternative.

avatar image f4bo · Nov 26, 2014 at 08:00 PM 0
Share

to help you out better then, there is something I would know more about your code though: where is this script attached to and where and how it is updated gui.coinsCollected?

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

7 People are following this question.

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

Related Questions

Spawn different Player models 0 Answers

player spawning 0 Answers

Camera cannot find instantiated player 1 Answer

Add spawn to script [Multiplayer] 0 Answers

how to make a character resume after going into a random battle? 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