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 /
avatar image
0
Question by Gaming Worlds · Dec 27, 2015 at 02:05 AM · scripting problemadsbooleaninteger

How to make Boolean not add more than one integer?

So I'm implementing ads into the game and the problem is that I have it set that when the character dies a certain amount of times, the ads shows up.

Heres the code so far:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Advertisements;
 
 public class AdsScript : MonoBehaviour {
     public float addsing;
     void Start()
     {
         addsing = 0f;
         DontDestroyOnLoad (this.gameObject);
 
 
     }
 
     void Update()
     {
         GameObject thePlayer = GameObject.Find ("fly");
         TouchScript touchScript = thePlayer.GetComponent<TouchScript> ();
 
         if (touchScript.dead)
             addsing += 1f;
         return;
 
         if (touchScript.dead && addsing == 3) {
             if (Advertisement.IsReady ()) {
                 Advertisement.Show ();
                 addsing = 0;
             }
         }
 
     }
 
 
 }


The problem is that whenever the boolean dead becomes true, the script doesn't just add one integer but keeps on adding.

Is there a way to make it only add one integer when dead becomes true?

Comment
Add comment · Show 1
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 PabloUnityArgentina · Dec 27, 2015 at 04:03 AM 0
Share

That's because the return statement it's making avoiding all code from line 25 to 35. So it never executes it. Try removing the return statement.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by corn · Dec 27, 2015 at 02:58 AM

In its current state, your script won't do what you intended. return; on line 22 is called every time, so nothing after that line will ever be reached, and all your script does is incrementing addsing while touchScript.dead is true.

This script could be fixed, but a more elegant solution would be to use events. That way, you don't have to check at every update if the player's dead, you just define a method that gets called whenever the player dies, which is what you wanted.

First, in TouchScript, create a UnityEvent and invoke it whenever the player dies.

 using UnityEngine.Events;
 
 public class TouchScript 
 {
         ...
         // Define a new UnityEvent
         public UnityEvent onDead = new UnityEvent();
 
         void Die() 
         {
                 ...
                 // Invoke it when the player dies
                 onDead.Invoke()
         }
 }

Then, in your AdsScript, you have to listen to this event and add a callback.

 void Start()
 {
          addsing = 0f;
          DontDestroyOnLoad (this.gameObject);
          
          // Use GameObject.Find only once
          GameObject thePlayer = GameObject.Find ("fly");
          TouchScript touchScript = thePlayer.GetComponent<TouchScript> (); 

          // Add listener to the event you just defined
          touchScript.onDead.AddListener(onPlayerDead);
 }

 void onPlayerDead()
 {
         // Called whenever the player dies
         addsing += 1f;
         if (addsing == 3) 
         {
              if (Advertisement.IsReady ()) {
              Advertisement.Show ();
              addsing = 0;
          }
 }


Comment
Add comment · Show 3 · 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 Gaming Worlds · Dec 27, 2015 at 03:20 AM 0
Share

Thanks! Yeah the return on there was by accident. I tried it out and it worked thanks again!

avatar image Gaming Worlds · Dec 27, 2015 at 03:36 AM 0
Share

Quick question: So it's not adding a number to addsing, do I have to tell it when the character dies? Sorry if this sounds dumb I'm new to program$$anonymous$$g. @corn

avatar image corn Gaming Worlds · Dec 27, 2015 at 03:48 AM 1
Share

Yes, just call the onDead event with onDead.Invoke() whenever the player dies.

You could also use a property to invoke the event whenever touchScript.dead becomes true :

 // Private variable holding the actual boolean value
 private bool m_Dead = false;
 
 // Property 
 public bool dead 
 {
     // Called whenever dead is read
     get 
     {
         return m_Dead;
     }
     // Called whenever dead is modified
     private set
     {
         m_Dead = value;
         if (value) 
         {
             onDead.Invoke()
         }
     }


avatar image
0

Answer by PabloUnityArgentina · Dec 27, 2015 at 09:21 AM

Never use GetComponent functions inside Update function unless it is really, really necessary.

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

34 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

Related Questions

Unity Ads Banner not working. 0 Answers

Setting bool from other script for specific gameobjects (while they're using same movement script) 2 Answers

How can I make the Random.Range in this spawning script a PUBLIC adjustable variable? The (0, 35) code limit gives errors when I need the prefab list to fluctuate. An example code would help. Thanks! 1 Answer

in rewarded ad which code means user closed the ad by itself 2 Answers

Boolean Troubles(C#)- Can anyone help? 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