• 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 /
  • Help Room /
avatar image
0
Question by K33n3r · May 08, 2016 at 02:59 PM · reference

Referencing Oddities

Greetings. I'm new to this whole scripting thing, but I'm learning bit by bit. I'm currently messing around with referencing and, while I have a basic understanding of how it works, my current script produces curious results. Script using the reference:

 using UnityEngine;
 using System.Collections;
 
 public class ObjectManager : MonoBehaviour {
 
     public PlayerController playerController; //script I am referencing
 
     AudioSource audioSource; //an audio source is attached to the game object holding this script
     
     void Start () {
         audioSource = GetComponent<AudioSource>();
     }
     
     
     void Update () {
         playAudio();
     }
 
     void playAudio(){
         if (playerController.count == 5){
             audioSource.Play();
         }
     }
 }

And here is the relevant section of PlayerController in regards to my "count" variable:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerController : MonoBehaviour
 {
     public int count;
 
     void Start(){     
         count = 0;
     }
 
  void OnTriggerEnter2D(Collider2D other){
         if (other.gameObject.CompareTag("Sprite")){
             other.gameObject.SetActive(false);
 
             count++;
         }
 }    

And of course, I dragged the object with the PlayerController script attached into the ObjectManager script component via Unity's inspector. Count is properly incrementing according to the public variable's inspector window. However, this code isn't working as expected. Here are the problems I have encountered:

  1. The Audio clip will not play unless "count" is one integer greater than the condition specified in the "if" statement. So "if (playerController.count == 5)" will not be true until score is 6 for some odd reason.

  2. If the condition is set to "if (playerController.count > 'any negative number')", "if (playerController.count == 0)", or "if (playerController.count < 'any positive number')", the audio clip will play immediately as expected, but only a fraction of it for a split second. Any increase in count does nothing after this point.

  3. The condition "if(playerController.count > '0 or any positive number')" does nothing when the condition is met.

Any idea why this code is responding so strangely?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · May 08, 2016 at 04:05 PM

You're calling audioSource.Play() in Update(), which is called every frame.

So, as soon as playerController.count == 5, you're playing your audio source 60 times a second (let's say for the sake of argument your game is running at 60FPS). Each new Play() restarts the audioclip, which is why you can't hear it - there's probably a slight silence at the beginning of your clip. But as soon as the condition is no longer true (i.e. when playerController.count==6), it doesn't restart the audio, which means you get to hear out the whole of the clip that was last called.

It's unclear what desired behaviour you actually want, but it might help you understand what is happening if you did something like this, to only restart the audio if it's not already playing:

      if (playerController.count == 5 && !audio.isPlaying){
          audioSource.Play();
      }
Comment
Add comment · 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 K33n3r · May 08, 2016 at 08:25 PM 0
Share

I had no idea that the clip would actually attempt to play every frame, I just wanted it to check every frame whether the condition was true. The odd results makes a little more sense now, and your solution worked perfectly. Thanks a ton, I can finally get back to my project ins$$anonymous$$d of worrying about this stupid little bug!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to find (and remove) references to GameObject C# 0 Answers

Instatiated object not being referenced in Start function? 2 Answers

Getting Int value from script to other script 2 Answers

I am new to unity and think that boo seems like the best language as it is very simple. Anyone know a good tutorial? 1 Answer

Why is my melee attack not working? 0 Answers


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