- Home /
Triggering GUI Button from collision with GameObject
Hi I'm trying to trigger 3 GUI Buttons when the player collides with an empty game object, but I am getting this error.
SendMessage Contact has no receiver! UnityEngine.Component:SendMessageUpwards(String) InterActing:OnTriggerEnter(Collider) (at Assets/Scripts/Controllers/InterActing.js:5)
On my empty game object I have this script
var SendMessageUpwards:GameObject;
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
SendMessageUpwards ("Contact");
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
SendMessageUpwards ("NoContact");
}
}
On my Player I have the following script
@script ExecuteInEditMode ();
var firstChoice = TextureGUI();
var secondChoice = TextureGUI();
var thirdChoice = TextureGUI();
var noGuiStyle : GUIStyle;
var soundOne : AudioClip;
var soundTwo : AudioClip;
var soundThree : AudioClip;
var isInteracting = false;
private var pathChoice:String = "";
private var screenCenter:Vector2;
function Contact() {
isInteracting = true;
pathChoice = "";
}
function NoContact() {
isInteracting = false;
}
function OnGUI () {
if (isInteracting) {
screenCenter.x = Screen.width/2;
screenCenter.y = Screen.height/2;
GUI.color = Color(.1,.1,.1,.8);
GUI.color = Color.white;
if (GUI.Button(Rect(screenCenter.x+firstChoice.offset.x,screenCenter.y+firstChoice.offset.y,firstChoice.texture.width,firstChoice.texture.height),firstChoice.texture,noGuiStyle)) {
pathChoice = "firstpath";
audio.PlayOneShot(soundOne);
RunCommand(pathChoice);
}
if (GUI.Button(Rect(screenCenter.x+secondChoice.offset.x,screenCenter.y+secondChoice.offset.y,secondChoice.texture.width,secondChoice.texture.height),secondChoice.texture,noGuiStyle)) {
pathChoice = "secondpath";
audio.PlayOneShot(soundTwo);
RunCommand(pathChoice);
}
if (GUI.Button(Rect(screenCenter.x+thirdChoice.offset.x,screenCenter.y+thirdChoice.offset.y,thirdChoice.texture.width,thirdChoice.texture.height),thirdChoice.texture,noGuiStyle)) {
pathChoice = "thirdpath";
audio.PlayOneShot(soundThree);
RunCommand(pathChoice);
}
}
}
function RunCommand(pathChoice:String) {
switch(pathChoice) {
case "firstpath":
isInteracting = false;
break;
case "secondpath":
isInteracting= false;
break;
case "thirdpath":
isInteracting = false;
break;
}
}
And do not know were I am going wrong in my code and would appreciate any help in solving this issue. Thank you
Answer by Lttldude · Apr 17, 2012 at 03:44 PM
I don't think you are using the SendMessageUpwards correctly. It is used to run functions on the parent(s) of an object. I assume your empty gameobject is not the child of teh player, thus this wouldn't work. Even if it was, you don't need to define the SendMEssageUpwards as a variable in the beginning, you would just do this
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.SendMessageUpwards ("Contact");
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.SendMessageUpwards ("NoContact");
}
}
However, that is only going to work if the player is a parent of the empty gameObject.
Otherwise for a less resourceful way, do this:
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.GetComponent("PlayerScriptName").Contact();
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.GetComponent("PlayerScriptName").NoContact();
}
}
OR if you really wanted to use SendMessage:
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.SendMessage("Contact");
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.SendMessage("NoContact");
}
}
Hope this helps. Be sure to tell me if this doesn't work.
Answer by Codebreaker73 · Apr 17, 2012 at 08:01 PM
Man thank you so much but ran into a few issues further issues.
var guideOne : AudioClip;
var objectOfAnimation : GameObject;
var guidePoint:AnimationClip;
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.SendMessage("Contact");
animation.Play("GuidePointingBehind");
audio.PlayOneShot(guideOne);
}
}
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
other.gameObject.SendMessage("NoContact");
animation.Play("GuideIdle");
}
}
if you have any ideas I really would appreciate it. But thank you again for the above solution.
Your welcome, glad to help. :) What issues are you having with this new code? And be sure to select the answer above as correct by using the checkmark/thumb up.
Oh duh my bad, the issue I'm running into is the animation is not playing and the audio continues to play when you exit the collider. Also is there a way to delay the GUI buttons appearing on the screen until the audio has finished? Thank you again.
Argh Never $$anonymous$$d figured out what I did wrong , forgot to add the animation to the character DUH , once again lttldude thank you
Your answer
Follow this Question
Related Questions
Trigger collision with Player won't work 2 Answers
gameObject.enabled is not working 1 Answer
GUI text like GTA 2 Answers
Coin pickup script not working..? 1 Answer
activate gui on trigger enter 1 Answer