Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Codebreaker73 · Apr 17, 2012 at 03:05 PM · collisionguitriggerbuttons

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lttldude · Apr 17, 2012 at 08:42 PM 0
Share

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.

avatar image Codebreaker73 · Apr 18, 2012 at 12:44 PM 0
Share

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.

avatar image Codebreaker73 · Apr 19, 2012 at 09:05 PM 0
Share

Argh Never $$anonymous$$d figured out what I did wrong , forgot to add the animation to the character DUH , once again lttldude thank you

avatar image maroonrs2 · Apr 19, 2012 at 09:31 PM 0
Share

make sure you have Is Trigged :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Trigger collision with Player won't work 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges