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 Mrknztrk · Aug 16, 2020 at 10:31 AM · errorcodepage

Same scripts don't work the same way on different objects

Hello everyone.

I'm trying to build a script whenever you die 5 times, the ad will appear, sound of the game will get muted, and whenever player is done with watching the ad and skips it, sound of the game will get unmuted. Alright, I achieved that, in a way. But there are only two way of dying in my game, one by falling (collision by deathline) and the other one by collision by enemy. My script with the former works great, when I apply my same script to the latter, it doesn't work. I'll share the code and then explain a bit more.

TLDR: the game sound is off during ad display while deathcount is incremented by deathfall and thus displaying ad(success), the game sound is still on in ad screen when deathcount is incremented by death by enemy and thus displaying ad, when it's supposed to be muted during ad(failure)

I'll add comments near codes to make it less complicated. I'd appreciate any kind of suggestions and helps as I am still at the very beginning of my learning and I am aware that I might have made things more complicated than they are supposed to be, as a result of my limited knowledge.

 public class AdManager : MonoBehaviour
 {
     public string GameID = "blablabla";
     public string InterstitialPlacementID = "blablabla";
     public bool testMode = true;
 
     private bool showInterstitial= false;
 
     public static int deathCount = 5;  // This is for ensuring the display of interstitial ad when you die for the first time as threshold is 6, after that it will be shown after 5 times.
 
     public GameObject DeathLine;  // so as I said I attach my script to 2 different objects. when I add my script to deathline, deathcount is incremented by player's collision with deathline after falling. when I attach this script to gameobject player, I attach my monster(enemy) to this part (deathline) in inspector so deathcount is incremented by player's collision with enemy.. think of it as deathline and enemy, the only killer objects.
     public GameObject player;
 
     bool toggle = true;  //sound is on.
     public GameObject deathScreen;  // the screen that appears when you die.
 
 
     void Start()
     {
         Advertisement.Initialize(GameID, testMode);
     }
 
     void Update()
     {
         OnDead();
 
         if (showInterstitial)
         {
             if (Advertisement.IsReady(InterstitialPlacementID))
             {
                 Advertisement.Show(InterstitialPlacementID);
                 deathCount++;
                 showInterstitial= false;
             }
         }
 
         if (!deathScreen.activeInHierarchy) // when the ad was over, the game sound was still muted, so i kinda found a solution, after ad is over and you click on replay, deathScreen is hidden in hierarchy and you get the sound back.
         {
             toggle = true;
             ToggleSound();  
         }
 
 
     }
 
     public void OnDead()
     {
         if (deathCount == 0)
         {
             showInterstitial= true;
             toggle = false;
             ToggleSound();
         }
 
         else if (deathCount == 6)
         {
             deathCount = 0;
 
         }
 
 
     }
 
 
     void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.tag == "Player") 
         {
             deathCount++;
         }
         if (collision.gameObject.tag == "Enemy")
         {
             deathCount++;
         }
 
     }
 
     public void ToggleSound()
     {
         if (toggle)
         {
             AudioListener.volume = 1f;
         }
         else
         {
             AudioListener.volume = 0f;
         }
 
     }
 
 }


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

0 Replies

· Add your reply
  • Sort: 

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

174 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 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 can I spawn different GameObjects using IEnumerator? 1 Answer

Why The player don't move? 0 Answers

Newbie Spawner C# Help 3 Answers

How do I close my game using the escape key? 2 Answers

Instantiated gameObject(here, wall) destroyed, but when checked for existence, shows that it exists, how? 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