- Home /
how to stop tag switching on same object?
So I am having an issue with the way my tag is being referenced, what seems to be happening is that when I look at the object tagged with "Face2" I am being returned on first pass "Face2" (this is what i'm expecting). When moving around the object the return tag switches between the tag "Face2" and "Face1".
On the opposite end I receive no errors when looking at the object tagged with "Face1" So I'm not sure if there is something wrong with the code I have written or if I have set my tags up incorrectly.
My first assumption was that I have a logical error but cant seem to find what it is I'm doing wrong, or that when called out in the code I am referencing this out incorrectly with the way it pulls from the hierarchy.
I have checked and confirmed that changing their position on the hierarchy has not worked.
any help from you 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
float Timer; // timer for amount of gaze time
public bool Hover = false;
public void OnPointerEnter(PointerEventData eventData) // is gaze creating a pointerevent
{
GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f); // assignes the values of random to the new color
string Face = gameObject.tag; // find gameobject that the OnPointerEnter
Debug.Log(Face);
Debug.Log("woah");
if (Face == "Face1") // comparing last press event with face_1 if true then
{
Face1_Audio.mute = false; // sound back to original
Face2_Audio.mute = true; // mute audio Still play
Debug.Log("hello,this is face1");
}
else if (Face == "Face2") // if not face_1 then chack to see if face_2
{
Face1_Audio.mute = true; // mute audio still play
Face2_Audio.mute = false; // sound back to original
Debug.Log("hello, this is face2");
}
else // if neather of these then do nothing
{
Debug.Log("neither Face1 or Face2");
}
//code seems to break right here only when these three lines below are place.
string What = EventSystem.current.currentSelectedGameObject.tag; // goes to the event system and finds the tag of the current selected game object
Debug.Log(What); // refuses to output any information
Debug.Log("this is the returned string of currentSelectedGameObject");
bool Still_there = EventSystem.current.IsPointerOverGameObject(); // looks in EventSystem to see if pointer is on a gameboject
if (Still_there) // looking to see if player is still gazing at the object, if they are do following code
{
Timer += Time.deltaTime; // noticed that time only increments when player moves around (think this as something to do with the way OnPointerEnter updates
Debug.Log("this is on pointer still there");
Debug.Log(Timer);
if (Timer == 1.0f)
{
Debug.Log("reached 1.0f");
if (Timer >= 1.0f)
{
Timer = 0.0f;
}
}
}
else if (!Still_there) // resets the timer to 0 (never enters this)
{
Timer = 0.0f;
Debug.Log("this is on pointer not there");
Debug.Log(Timer);
}
}
}
Answer by Sek19 · Dec 13, 2015 at 09:12 PM
what the problem was, is that the original game object was duplicated. For some reason the duplicated object was referencing both the Face1 & Face2 tag even though changed in the tag setting. All that needed to be done was create the same gameobject again without duplication and everything runs smoothly.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Object Reference not set to an Instance 2 Answers
Object reference not set to an instance of an object. 1 Answer
Set reference to multiple gameobjects based on tag category / multiple tags? 2 Answers
Help With LookAt Target 1 Answer