what am I doing wrong in if statement comparison
Hello all and wonderful day to you! I have been writing a script that takes a cube, changes its color with a random one Then is supposed to find a gameObjects tag on the cube. If the cube has face1 as its tag then it should turn Face1_Audio back to original audio volume (unmute). while it turns Face2_Audio to mute. and vise versa for the else if. I'm not sure if I'm calling for the game tag incorrect or if I should be referencing this in another manner with the if statement. As of right now this is unresponsive besides the changing of the color so I have narrowed it down to these if statements. All help or advice is much appreciated!
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
public class Cameleon : MonoBehaviour, IPointerEnterHandler
{
public AudioSource Face1_Audio; // calling from audio source 1
public AudioSource Face2_Audio; // calling from audio sourc 2
GameObject gameobject;
public void OnPointerEnter(PointerEventData eventData) // is gaze creating a pointerevent
{
string Face = gameObject.tag;
// PointerEventData lastPress; // the GameObject for the last press event
GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f); // assignes the values of random to the new color
if (Face == "Face1") // comparing last press event with face_1 if true then
{
Face1_Audio.mute = true; // sound back to original
Face2_Audio.mute = false; // mute audio Still play
}
else if (Face == "Face2")
{
Face1_Audio.mute = false; // mute audio still play
Face2_Audio.mute = true; // sound back to original
}
}
}
Answer by centaurianmudpig · Dec 02, 2015 at 04:25 PM
You have muted and unmuted audio without trying to Play() it. Also, rather than muting I would use Stop().
I'm sorry I must not have explained This correctly. I do not want to stop the audio due to the fact I need it to play while not heard to keep up with some of the ti$$anonymous$$g. So i feel as though I do not need a Play() as the audio is already playing on loop on start. Now I'm not certain if the Play() is essential to muting if the audio is playing already but what are your thoughts on the matter?
After you have assigned face to the tag, print out the value to console and confirm the value is what you expect. This will also confirm if OnPointerEnter() is being called.
string Face = gameObject.tag;
Debug.Log(Face);
thank you for that, So this is what the Console returns.
Cardboard SD$$anonymous$$ object should be a singleton. UnityEngine.Debug:LogWarning(Object)
definitely not what I was expecting as a return. I ended up changing the code to this
if (Face == "Object")
{
Face1_Audio.mute = false; // mute audio still play
Face2_Audio.mute = false; // mute audio Still play
}
just to see if the audio would turn off. Even with this absolute of Face = Object it does nothing.
Is this because it's not actually returning a string? I'm not sure why it's not returning the actual tag. all objects that the OnPointEvent would hit in scene are marked with tag (Face1) in the inspector.