- Home /
Help with ontrigger enter and exit with UI Display appear and disappear
Ok so I am having a lot of trouble with this script I've written for my game. I'm almost done with it and this is one of the last pieces of the puzzle. I have a first person maze game, and I want the player to interact with a game object that ends up being a game point if the question is answered correctly. I only want the UI Panel to come up if the player enters the collider of the game object (game point). Then once the UI Panel comes up I want them to be able to answer the question asked by either keying in Y for yes or N for no. That or choosing a Yes button or a No button. I feel like i'm really close with my script but I keep getting this error (posted below with script) can anyone help me please.
Static member `UnityEngine.GameObject.FindGameObjectWithTag(string)' cannot be accessed with an instance reference, qualify it with a type name instead
script is listed below
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections;
public class QuestionsManager : MonoBehaviour { public GameObject Question1;
// Use this for initialization
void Start ()
{
Question1.FindGameObjectWithTag ("Level1Q1").SetActive = false;
}
void OnTriggerEnter()
{
Question1.FindGameObjectWithTag ("Level1Q1").SetActive = true;
}
void Update()
{
if (GetComponent<Collider>().gameObject.tag == "Level1Q1" && Input.GetKeyDown (KeyCode.N)) {
Debug.Log ("That is the correct answer");
Question1.FindGameObjectWithTag ("Level1Q1").SetActive = false;
} else if (GetComponent<Collider>().gameObject.tag == "Level1Q1" && Input.GetKeyDown (KeyCode.Y)) {
SceneManager.LoadScene ("WrongAnswerLevel1Q1");
}
}
void OnTriggerExit()
{
Question1.FindGameObjectWithTag ("Level1Q1").SetActive = false;
}
}
Answer by Zoogyburger · Aug 13, 2016 at 11:40 PM
Try this instead:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class QuestionsManager : MonoBehaviour { public GameObject Question1;
// Use this for initialization
void Start ()
{
Question1 = GameObject.FindGameObjectWithTag ("Level1Q1");
Question1.SetActive (false);
}
void OnTriggerEnter()
{
Question1 = GameObject.FindGameObjectWithTag ("Level1Q1");
Question1.SetActive (true);
}
void Update()
{
if (GetComponent<Collider>().gameObject.tag == "Level1Q1" && Input.GetKeyDown (KeyCode.N)) {
Debug.Log ("That is the correct answer");
Question1 = GameObject.FindGameObjectWithTag ("Level1Q1");
Question1.SetActive (false);
} else if (GetComponent<Collider>().gameObject.tag == "Level1Q1" && Input.GetKeyDown (KeyCode.Y)) {
SceneManager.LoadScene ("WrongAnswerLevel1Q1");
}
}
void OnTriggerExit()
{
Question1 = GameObject.FindGameObjectWithTag ("Level1Q1");
Question1.SetActive (false);
}
}
Your answer
Follow this Question
Related Questions
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
Can someone explain calling other scripts in C#? 2 Answers
Quick code help! (c#) 2 Answers
Spawning 3D Objects according to Data at JSON Matrix 0 Answers
I need some help with my high score.,I need help with my highsore. 1 Answer