Search Unity

I will transfer $20 to the first persons paypal account that can solve why this is not working.....

Discussion in 'Scripting' started by Unlimited_Energy, Aug 29, 2015.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4.  
    5. public class AudioM : MonoBehaviour{
    6.  
    7.  
    8.  
    9.     public void AddClip ()
    10.     {
    11.  
    12.         GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    13.         AudioSource audio = GetComponent<AudioSource> ();
    14.  
    15.         if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
    16.             if (gameObject.tag == "Button2"){
    17.  
    18.                 audio.clip = (AudioClip)Resources.Load("Sword1");;
    19.             }
    20.         }
    21.  
    22.     }
    23. }
    24.  
    So I tried to do the onclick on Button2 and call my scripts function AddClip to see if my script will even work in the most basic way by having Button2's OnClick call the function AddClip to change the AudioClip on the Audio Source Component on Button2 to "Sword1" audio file from my resources folder and it still will not laod Sword1 into the AudioClip property of the AdudioSource Component on Button2 on the condition that my UI game Image called Slot1 has a SourceImage of Sword.

    Something is not working.I think the reference to Slot1 is ok, im not sure, but I have a feeling the entire resource load, variable decleration and such is in the worng place and not written correctly.
     
  2. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Ok, hold on. What exactly are you trying to do? I could not understand your post.
     
  3. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    I have a Button called Button2 and I have a script attached to Button2 called AudioM that checks to see if another game object called Slot1, (which is a UI Image) Source Image is Sword and if that is True then the AudioClip on Button2's AudioSource COMPONENT will change to Sword1.wav file from my resources folder. I have spent the past two weeks trying every code combination on earth and I cannot figure this out.
     
    Last edited: Aug 29, 2015
  4. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Try this:
    1.First check to see if the Slot 1 is not inactive.
    2. Make sure you have an audioSource on the button.
    3. Make sure the image on Slot1 has the image of Sword & is named "Sword"
    4.Make sure the RESOURCES folder is named that and has no extra folder in it.
    5.Make sure the audio file is inside of this folder named "Sword1"
    6. Try this:
    Code (CSharp):
    1. //When you click the button
    2. public void AddClip(){
    3.  
    4.     //Grab the slot
    5.     GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    6.     //Audio on the button
    7.         AudioSource audio = GetComponent<AudioSource> ();
    8.     //Check the image for the sprite name (Make sure the sprite is named "Sword")
    9.         if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
    10.     //Remove this line here if(gameObject.tag == "Button2"){
    11.          //Load audio clip with the name Sword1
    12.                 audio.clip = (AudioClip)Resources.Load("Sword1");
    13. //And this line here }
    14.         }
    15. }
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm going to suggest taking a step back and ask why. What are you actually trying to achieve. This seems to be a very convoluted way to do the job at hand. What in game effect are you trying to achieve?

    Some general red flags I see:
    • Why use resources instead of an inspector reference?
    • Why use find instead of an inspector reference?
    • Why are you checking the sprite name? Why not assign the audio at the same time as the sprite?
    • Why are you tag checking this button?
    A description of what is not working also helps. Are you getting compiler errors? Do debug statements show the function is being called? Are you getting inside each if?

    If you want to engage me as a freelancer and send me the project to fix this script, then PM me. But I'm not going to jump in to a scripting competition here.
     
  6. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    checked everything its not working. One thing, how do I see if slot1 is active or not? im not sure what you mean.
     
  7. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    When you look at the Slot1 gameObject, check to see if, in the Inspector, if the checkbox at the top-left is checked or not. If it is checked, it is active, if not then it is not.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Do some debugging on your original code. Like so:

    Code (CSharp):
    1. public class AudioM : MonoBehaviour{
    2.  
    3.     public void AddClip ()
    4.     {
    5.         Debug.Log ("Entering AddClip");
    6.         GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    7.         Debug.Log ("Slot1: "+ tmpGO, tmpGO);
    8.         AudioSource audio = GetComponent<AudioSource> ();
    9.         Debug.Log ("audio: "+ audio, audio);
    10.  
    11.         if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
    12.             Debug.Log("Entering sprite if");
    13.             if (gameObject.tag == "Button2"){
    14.                 Debug.Log ("Entering Button2 if");
    15.                 audio.clip = (AudioClip)Resources.Load("Sword1");;
    16.             }
    17.         }
    18.  
    19.     }
    20. }
    The results will tell you which part of your code is failing. Debugging is an incredibly important skill as a programmer.
     
    Unlimited_Energy likes this.
  9. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    the reason I need to do it my way and not assign in the inspector is because there is going to be ALOT more to my script and for simplicity sake I wrote a very short function for what Im trying to acheive.

    I have 36 buttons that will need to have there audioClip changed to a wav file based on what source image is assigned to Multiple UI images. I do not want to over complicate what Im asking by adding more to the mind of people helping me because then people will start writing up things that I don't currently want answered. I need to focus on WHY my script in its current state does not work.

    This will solve my current issue and also show me how any other script I write can access components Reference Properties. This very basic script is not working and it is a mystery as to why its unable to change the AudioClip..
     
  10. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    To check a UI image, doesn't that sprite need to have Read/Write enabled?
    Choose the sprite (Sword), change it from Sprite (2D and UI) to Advanced, and tick Read/Write Enabled.

    Click apply and see how to goes.
     
    Unlimited_Energy likes this.
  11. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469

    it is checked.
     
  12. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Good point.
     
    Unlimited_Energy likes this.
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Okay, I see now. Is it the name checking on line 15 that's throwing you out? Sprite.name returns the name of the GameObject, not the name of a sprite.
     
  14. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    changed it to advanced and hit read/write but still nothing
     
  15. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Not true. I used this before and it always gave me the sprite name.
     
  16. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Nah, that's just if you want to read and write individual pixels off of the texture.
     
  17. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469

    I thought that it could be the issue also. Im not sure how to check the sprite name then if that code does not work. and YEs I need to see if the actual source image's file is named Sword and if the Sword image is in the SourceImage property on Slot1 then change Button2s AudioClip to Sword1.wav. It seems like such a simple task that has turned out to be soo mind numbly hard to solve.
     
  18. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The debug statements I suggested before will clear it up. I typically wouldn't rely on the name of an object. I would add another field that stores the objects details.
     
  19. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Cant you do this?
    Code (CSharp):
    1. sprite.texture.name;
    or something like it.
     
  20. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    did not work
     
  21. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Entering AddClip
    UnityEngine.Debug:Log(Object)
    AudioM:AddClip() (at Assets/AudioM.cs:11)
    UnityEngine.EventSystems.EventSystem:Update()
    -------------------------------------------
    Slot1: Slot1 (UnityEngine.GameObject)
    UnityEngine.Debug:Log(Object, Object)
    AudioM:AddClip() (at Assets/AudioM.cs:13)
    UnityEngine.EventSystems.EventSystem:Update()
    -----------------------------------------------------
    audio: Button2 (UnityEngine.AudioSource)
    UnityEngine.Debug:Log(Object, Object)
    AudioM:AddClip() (at Assets/AudioM.cs:15)
    UnityEngine.EventSystems.EventSystem:Update()
    ---------------------------------
     
  22. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Let me try something. Give me a minute.
     
  23. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    take all your time guys. Im here watching the thread. I just want to solve this thing lol.
     
  24. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I stand corrected, you are right.

    I just ran the script in Unity. Despite it's convoluted nature, it does work, if you have every tag and name right. But I would suggest something much simpler, as in:

    Code (CSharp):
    1. public void AddClip ()
    2. {
    3.         GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    4.         AudioSource audio = GetComponent<AudioSource> ();
    5.         audio.clip = Resources.Load<AudioClip>(tmpGO.GetComponent<Image>().sprite.name);
    6. }
    General comment, why is the audio source on your UI.Buton?
     
  25. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This indicates the code is failing on the line with the if spite.name = sword. Put a debug.log immediately before this line to see what the name if the sprite really is.
     
  26. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    I was just about to make this script... 0_o
    You should put the audioSource on the camera or a seperate gameObject.
     
  27. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    audio.clip=Resources.Load<AudioClip>(tmpGO.GetComponent<Image>().sprite.name);
    2 questions.
    1. is the (tmpGO.GetComponent<Image>().sprite.name); part of the line of code the arguement meaning if this is ture then load the audioclip?
    2. where is the check to see if Sword is the sprites name?
     
  28. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    1. No. I'm passing the name of the sprite into Resources.Load directly. This solves your next problem of "How do I make this work for all 35 sprites in my game."

    2. Similar to the above, you don't. This code will load the audio clip that matches the name of the sprite. So if the sprite is sword the sword audio will be loaded. If it's wand the wand audio will be loaded. And so forth.
     
    Unlimited_Energy likes this.
  29. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Nice! :)
     
    Unlimited_Energy and Kiwasi like this.
  30. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    i did not work but maybe you guys are right.

    I was under the assumption that the audiosource component needed to be attached to the button to hold the audio clip.

    heres what Im doing. I have 6 buttons Button1-6. I need to attach the same script to each button that has a Function we will call PlayClip. When I click any of the 6 buttons it needs to check Slot1s source image if it is a Sword and im clicking button1 it will play sword1.wave, clicking button 2 will play sword2, clicking button3 will play sword4.wav ect. if UI source image is a Bomb then clicking Button1 will play bomb1, clicking button 2 will play bomb2
     
  31. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm just making guesses based on what I can see. If the audio source plays with the button, then by all means, leave it there. Remember I don't have your game in front of me.
     
  32. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Very Nice code, however I have 6 Audio files for each ui image. so buttons 1-6 need to have Sword1-6.wav assigned
     
  33. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Easy fix. Here is a hack that matches how you have been coding this. I personally would use a loop on a single component instead.

    Code (CSharp):
    1. [SerialiseField] int buttonNumber;
    2.  
    3. public void AddClip ()
    4. {
    5.         GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    6.         AudioSource audio = GetComponent<AudioSource> ();
    7.         audio.clip = Resources.Load<AudioClip>(tmpGO.GetComponent<Image>().sprite.name + buttonNumber.ToString());
    8. }
     
    Unlimited_Energy likes this.
  34. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    im going to try this in a min cooking breakfest.
     
  35. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    All good. It's late here, I'm going to crash. But I'll check back in the morning and see how you went.
     
    Unlimited_Energy likes this.
  36. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Thank you, i forgot to tell you tho that I have 6 buttons that pertain to 6 slots. so i need 6 audio foles to get distributed to a single ui image based on if that ui images source image. so if slot one has a sword ui then the 6 audio files for sword need to be assigned to slot 1.
     
  37. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Run a for loop for each audio clip.
     
  38. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I just tested the OP code, and it worked fine, with the scene set up in the way that I'd expect it to be
    If it's failing for you, something is set up wrong


    Question: Is this:
    what you got for debug log from this suggestion?
    If so, then clearly the if-test on OP line 15 failed, and the sprite name is not "Sword", because it never logged "Entering sprite if"
    add in line 14
    Debug.Log(tmpGO.GetComponent<Image>().sprite.name)
    and post screenshots of the relevant bits
     
    Unlimited_Energy and Kiwasi like this.
  39. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That was my conclusion. But the code was messy enough that I attempted to suggest a better structure.

    However I'm beginning to think this may be a case for referring to the learn section and the yellow book.
     
  40. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Im getting this error code
    error CS0246: The type or namespace name `Button2Attribute' could not be found. Are you missing a using directive or an assembly reference?
     
  41. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Reading his/her post above I am beginning to think this is just trolling.
     
    Kiwasi likes this.
  42. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's nothing from my code...