- Home /
Show panel when collide on prefabs
Hey guys. I am making a RPG game. I have prefab wherein the player can pick-up that object. My question is, why does my panel is not showing when my character collides to a prefab (pickupable object). My prefab's collider (box collider) isTrigger was checked and also the panel is a prefab. The script below is attached to my prefab (pickupable object). These are the codes that I used:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DestroyFlower : MonoBehaviour {
private bool isPickedUp = false;
private float destroyTime = 1f;
public GameObject messagePanel;
void Update() {
if(isPickedUp && Input.GetKeyDown(KeyCode.LeftControl)) {
QuestManager.questManager.AddQuestItem ("Find 10 flowers", 1);
Destroy (gameObject, destroyTime);
}
}
void OnTriggerEnter (Collider other) {
messagePanel.SetActive (true);
if(other.tag == "Player") {
isPickedUp = true;
messagePanel.GetComponentInChildren<Text>().text = "Press Left-Control to pick up";
}
}
void OnTriggerStay (Collider other) {
if(other.tag == "Player") {
isPickedUp = true;
}
}
void OnTriggerExit (Collider other) {
if(other.tag == "Player") {
isPickedUp = false;
messagePanel.SetActive (false);
}
}
}
please remember that to active OnTriggerEnter you need to either of the two must to have a rigidody with use gavity and is kinematic disabled also the panel is a prefab but is on scene (inside canvas)? or is just on assets folder...? maybe is some like that, because don't seem to be a scripting error
Pd: a error is, if you press left control like a maniac close the flower before pass this second that take to destroy it, you can collect 10 at once xd
$$anonymous$$y character has a rigidbody and my panel prefab is on the resources folder. What should I do?
you character probably have a "use gravity" property on the rigidbody enabled, this cause that OnTriggerEnter cant be called
because is hard on most of the games have a character that ignore the gravity, add a rigidbody to the flowers and uncheck the use gravity that is enabled by default
also add the panel on scene, as child of canvas (you probably already have a canvas on scene) and disable it, you know uncheck the upper toggle on inspector, and also drag & drop the panel on scene (from hierachy to inspector) to the public variable messagePanel
like you did with the prefab (from assets to inspector).
maybe I over explained things that you already know, sorry
I did that but my panel is not showing. I've check it on the debug log if it is triggering then I got response.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Anyone know why my InputField is not being disabled? 0 Answers
Unity 4.6 Error different layout! 1 Answer
Circular scrolling Main Menu. 0 Answers