Search Unity

Sounds dont work

Discussion in 'Scripting' started by Xanis, Nov 27, 2014.

  1. Xanis

    Xanis

    Joined:
    Nov 27, 2014
    Posts:
    6
    Hi. I'm a beginner in Unity/C# and got a problem with sounds. None of them want to work. I receive an error in line 32 and 42 that only assignment, call, increment etc can be used as a statement. Could anyone help me to fix it?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sounds : MonoBehaviour
    5. {
    6.     public AudioClip footstepgravel;
    7.     public AudioClip breathjump;
    8.     public AudioClip fastdeath;
    9.     public AudioClip explosion;
    10.     public AudioClip cannonshot;
    11.  
    12.     public void Character()
    13.     {
    14.      
    15.         if(Input.GetKey("a") || Input.GetKey("d"))
    16.         {
    17.             audio.clip = footstepgravel;
    18.             audio.Play();
    19.         }
    20.  
    21.         if(Input.GetKeyDown("w"))
    22.         {
    23.             audio.clip = breathjump;
    24.             audio.Play();
    25.         }
    26.  
    27.         if (GetComponent<Character>().Dying == true)
    28.         {
    29.             audio.clip = fastdeath;
    30.             audio.Play;
    31.         }
    32.  
    33.         if (GetComponent<Character>().Dead == true)
    34.         {
    35.             audio.clip = explosion;
    36.             audio.Play();
    37.         }
    38.  
    39.         if (GetComponent<RangedWeaponController>.Shoot.projectileType == ProjectileType.bullet1)
    40.         {
    41.             audio.clip = cannonshot;
    42.             audio.Play;
    43.         }
    44.     }
    45.  
    46.     void Update()
    47.     {
    48.         Character();
    49.     }
    50.  
    51. }
     
    Last edited: Nov 27, 2014
  2. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Line 30 and line 42: it's 'audio.Play();' not 'audio.Play;'

    You forgot the open/closed parenthesis on the function Play.
     
  3. Xanis

    Xanis

    Joined:
    Nov 27, 2014
    Posts:
    6
    @AnimalMother you're right, now in line 39: expression denotes a method group, where a variable, value etc was expected. Any idea how to fix it? I'll past a code from a script:
    RangedWeaponController.cs:
    Code (CSharp):
    1. public enum ProjectileType
    2. {
    3.     bullet1
    4. }
    5.  
    6. public class RangedWeaponController : MonoBehaviour {
    7.     GameObject bullet1;
    8.     // Use this for initialization
    9.     void Start () {
    10.         bullet1 = Resources.Load<GameObject>("Prefabs/Bullets/bullet1");
    11.     }
    12.     public void Shoot(ProjectileType projectileType)
    13.     {
    14.         if(projectileType == ProjectileType.bullet1)
    15.         {
    16.             GameObject tmpBulletGO = (GameObject)Instantiate(bullet1);
    17.             tmpBulletGO.transform.position = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
    18.             if(gameObject.transform.parent.transform.lossyScale.x < 0)
    19.             {
    20.                 tmpBulletGO.rigidbody2D.velocity = new Vector2(-30, 0);
    21.             }
    22.             else
    23.             {
    24.                 tmpBulletGO.rigidbody2D.velocity = new Vector2(30, 0);
    25.             }
    26.            
    27.         }
    28.     }
    29. }
     
    Last edited: Nov 27, 2014
  4. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Not quite sure what you're trying to do in line 39, projectileType in the function Shoot is a parameter of that function, not a variable. Plus, you forgot your open/closed parenthesis after the GetComponent<RangedWeaponController>().

    Maybe the RangedWeaponController is incomplete or I'm not understanding the code, but what it looks like you want is a new variable in the RangedWeaponController of type ProjectileType. Then line 39 would look like this:

    if( GetComponent<RangedWeaponController>().currentProjectileType == ProjectileType.bullet1 )

    Where "currentProjectileType" is defined in the RangedWeaponController as a public ProjectileType. Then you can change this variable on the game object's component to declare it's bullet type.
     
  5. Xanis

    Xanis

    Joined:
    Nov 27, 2014
    Posts:
    6
    Sure i've pasted the full code in previous post this time. In line 39 i want to play the sound of weapon for a certain type of bullet when a player shots
     
    Last edited: Nov 27, 2014
  6. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Yea, so are you still getting an error or something? What I described before should work.

    So add the following line of code:

    Code (CSharp):
    1. public class RangedWeaponController : MonoBehaviour {
    2.     GameObject bullet1;
    3.     // ADDED THIS VARIABLE, defines this weapon's projectile type
    4.     public ProjectileType currentProjectileType;
    5.     // Use this for initialization
    6.     void Start () {
    7.         bullet1 = Resources.Load<GameObject>("Prefabs/Bullets/bullet1");
    8.     }
    Then change line 39 as I described before, should work now.
     
  7. Xanis

    Xanis

    Joined:
    Nov 27, 2014
    Posts:
    6
    Done the game runs, but i still only hear a background music added via inspector.
     
  8. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    Oh, I see now. There is no logic that tells the audio to actually play. You need to do some sort of if( GetComponent<RangedWeaponController>().IsShooting() ) or something that returns true if the gun is shooting.

    Also, make sure you assign the public AudioClip's in the inspector.

    Are you getting any sounds? Just not the weapon sound?
     
  9. Xanis

    Xanis

    Joined:
    Nov 27, 2014
    Posts:
    6
    Meh still the effect is the same with IsShooting bool variable. Ive assigned audioclips in the inspector earlier. Like i said i only hear the background music added via inspector, no more no less.
     
  10. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161
    One thing I can suggest from 10 years of programming is when a problem like this arises, ignore all other code. Create a small (and I mean very small) test bed, nothing more than the lines of code you are stuck on, and just get it to work. So in this particular case, simply create a game object with an audio source and a script with maybe some code like:

    Code (CSharp):
    1. public AudioClip m_testClip;
    2.  
    3. void Update()
    4. {
    5.    if( Input.GetKeyDown( "A" ) )
    6.    {
    7.       audio.clip = m_testClip;
    8.       audio.Play();
    9.    }
    10. }
    When you press A, it should play the test sound. If not, mess with it until it does. Search through the documentation. Here's an example search on Google: "unity audio source", that brought me here: http://docs.unity3d.com/ScriptReference/AudioSource.html

    and then to here through that link: http://docs.unity3d.com/ScriptReference/AudioSource-clip.html

    This is the only thing us programmers do all day; we search the all-mighty Google until our answers are found. If necessary (but personally, rarely for me) we use forums such as this when the problem gets particularly sticky. In your case, this seems like you simply have an issue with how the 'audio' component works on a game object. When a problem such as this arises, all programmers become scientists, and will create a similar test bed as I've described above. They keep it simple and play until they understand how the code works and how to accomplish the task. Once the code works as expected, then they begin to add logic, or bring the working code (and their new-found knowledge) back into the original code.

    There are few differences between an experienced and an inexperienced programmer, to me the biggest difference (to go from a 'beginner' to a 'novice' in my book, anyway) is the ability to determine the actual problem in the code, simplify it, and then using Google/technical docs/forums/youtube/prayer(joking)/etc. to answer this problem.

    This was long winded, but as we were talking I began to notice you were 'me' as a programmer many years ago, and this is the advice I'd have given myself at that time. Good luck!
     
  11. Xanis

    Xanis

    Joined:
    Nov 27, 2014
    Posts:
    6
    This time ive tried with the easiest code to just play a sound without any 'if' statements and unfortunately still no result. If you still want to help me, i can send you the whole project. If not, thats ok. Anyway thanks for help.
     
  12. OpticalOverride

    OpticalOverride

    Joined:
    Jan 13, 2013
    Posts:
    161


    Haven't watched this one, but if it doesn't teach you what you need to know, look up another (and another, and so on) until you learn what you need.

    My previous post (the last few paragraphs) was a sort of "teach a man to fish" type explanation. The title of my last post very well could have been: 'Programming: How to Get Your Hands Dirty'. People on these forums, with myself included, generally aren't going to help you by holding your hand with the simple stuff. You need to learn to use resources like Youtube, Google, and the Unity Docs to answer the majority of your beginner (and even novice/advanced) questions, as these forums are mostly here to answer strange/unique and often more advanced questions. For beginner questions, most people on this forum will say "find a Youtube tutorial on Unity", or send a link; hence the url at the top of this post. These links are meant for the truly lost programmers, and as a nudge in the right direction. If you aren't dedicated enough to search these resources out on your own, there's a chance you aren't as passionate about your project as you once thought. What I've said isn't meant to discourage, but to teach you how all real programmers learn the craft. In summation: you must learn how to learn.

    Hope this helps with your programming endeavors!
     
  13. Daniel-Greenhorn

    Daniel-Greenhorn

    Joined:
    Nov 6, 2014
    Posts:
    3
    I've hit the same problem (sort of, anyway): load a texture and play the sound if event occurs. Either load works fine alone but not together.

    void the_event (int the_input)
    {
    obj =GameObject.Find("Box");
    obj.renderer.material.mainTexture =Resources.Load(matrix [index, x]) asTexture;
    sound = matrix [index, y];
    audio.clip =Resources.Load(sound) as AudioClip;
    audio.Play();
    }

    By matrix [index, x] I meant the string at row "index" and the relevant column "x" for textures in a 2D array.
    If I disable either load segment (//) the other works correctly.
    Both loads don't work together (either object gets a white texture (null, I believe?!) and the sound plays or, if there is no sound by that name, the texture is applied correctly).

    What is the problem? Or, better yet, how would you do it, in this case?

    Thanks

    P.S. sorry about hijacking Xanis's post but this is the only related recent thread I've found on this subject