using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class AudioM : MonoBehaviour{
public void AddClip ()
{
GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
AudioSource audio = GetComponent<AudioSource> ();
if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
if (gameObject.tag == "Button2"){
audio.clip = (AudioClip)Resources.Load("Sword1");;
}
}
}
}
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.
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.
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:
//When you click the button
public void AddClip(){
//Grab the slot
GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
//Audio on the button
AudioSource audio = GetComponent<AudioSource> ();
//Check the image for the sprite name (Make sure the sprite is named "Sword")
if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
//Remove this line here if(gameObject.tag == "Button2"){
//Load audio clip with the name Sword1
audio.clip = (AudioClip)Resources.Load("Sword1");
//And this line here }
}
}
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.
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.
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…
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.
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.
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.
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.