Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mibbit · Dec 21, 2014 at 10:03 AM · sendmessageoncontrollercolliderhit

Mario style jump on enemy

I'm making what is at this point a 2D Mario clone and want to send a message initiating a method on an enemy script. The most efficient way I can think to do this would be to do as so.

I have a child object with a box collider and script for ground checking with this C# code.

 using UnityEngine;
 using System.Collections;
 
 public class MarioFeet : MonoBehaviour {
     
     
     void OnControllerColliderHit(ControllerColliderHit hit) {
         var hitVec = hit.point - transform.position;
         
         if (hitVec.y < 0) {
             Debug.Log("Boop");
             hit.gameObject.SendMessage("MarioStomp");
         }
     }
 }

Then on my npc object a sprite renderer, box collider and behavior script:

 using UnityEngine;
 using System.Collections;
 
 public class npcBehavior : MonoBehaviour {
 
     void MarioStomp() {
         Debug.Log("Boop-Bop");
     }    
 }
 

I'm not logging what I want but I'm getting a warning, "The referenced script on this Behaviour is missing!"

Do I need to find the game object in my script and get component? I'm not sure exactly how the hit or send message works as I'm new to unity would like to at least know where to look as I've found myself alone and down an alley I don't fully understand.

P.S. this is my first unity question so if there is anything I can do to make my question better I'd love to know.

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
0

Answer by Mmmpies · Dec 21, 2014 at 11:37 AM

Try making MarioStomp public

 public void MarioStomp()


EDIT:

OK I was on my phone with intermittent connection when I posted that so I went for the short answer in the hope that would fix it so now I have a good connection...

In MarioFeet instead of

 hit.gameObject.SendMessage("MarioStomp");

do this, you'll need to set a script reference at the top of the file first..

 private npcBehaviour npcBehaviourScript;

The where the send message is do this (instead)..

 npcBehaviourScript = hit.GetComponent<npcBehaviour>();
 npcBehaviourScript.MarioStomp();

So it picks up the script attached to the hit enemy and calls the MarioStomp() function.

Really that was shorter than I thought it would be, should have posted it first time.

EDIT:

In fact, thinking about it, you could do it all on one line because you only reference that script once so no point holding onto it. So instead of the SendMessage line..

 hit.GetComponent<npcBehaviour>().MarioStomp();

Should work as well.

Comment
Add comment · Show 1 · 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 mibbit · Dec 23, 2014 at 07:35 AM 0
Share

I tried this but still no difference in result. That is good advice though as I should have made sure the method was public before trying to interact between scripts components/objects.

Still hoping somebody can provide further insight.

avatar image
0

Answer by FoXISBacK · Dec 23, 2014 at 09:00 AM

The warning seems about the script itself, not the method you are trying to call. Sometimes even removing the script from the object and re-adding it does the trick.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Vector 3 Angles 2 Answers

SOLVED: Problem with SendMessage -- silently failing 2 Answers

Communicating between objects without any physical connection? 3 Answers

iPad touch scripting... 0 Answers

Send Message not working! 1 Answer


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