• 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 Henri 1 · Apr 04, 2011 at 10:06 PM · sendmessagejava-is-not-javascript

Send message from Java to C#

Hi There,

I'm trying to send a message from Java to C#, but I cant get the right coding for C#.

In JavaScript I do:

function BlastCollided () {
beam.SendMessage ("DoExplode");
}

How do I call the "DoExplode" in C#?

I need to tell the script to

Explode();

I tried this example from Unity Scripting reference, but got the following errors:

A get or set accessor expected

`Detonator.DoExplode': property or indexer cannot have void type

Anyone care to sooth this pain of mine?

EDIT: If for example I want to destroy my object when the receivers gets the message in Java, I would have done the following:

To send the message from object A:

    if input.GetButtonUp("Fire1") {
  ObjectB.sendmessage("DoExplode");

}

To Receive the message from Object A and do something

function DoExplode {
 MyObject.Destroy();

}

But now, I want to do exactly the same, but only in C#

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
2
Best Answer

Answer by Statement · Apr 04, 2011 at 10:26 PM

// In C#

void BlastCollided () { beam.SendMessage ("DoExplode"); }

// Full C# Example script:

using UnityEngine; class Example : MonoBehaviour { public GameObject beam; // Or public Component beam;, or what type you have void BlastCollided () { if (!beam) print("You have forgot to set 'beam' variable");

     beam.SendMessage ("DoExplode");
 }

}

Comment
Add comment · Show 11 · 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 Henri 1 · Apr 05, 2011 at 06:31 AM 0
Share

The Send $$anonymous$$essage Works fine, you can call the message anything you want, if you want to receive the message in Java, you use:

function Anything () { do.something; }

But does not work in C#

avatar image Henri 1 · Apr 05, 2011 at 06:32 AM 0
Share

Does not matter which component it is, I'm only trying to receive a message in C# and call any action

avatar image Statement · Apr 05, 2011 at 11:36 AM 0
Share

Sorry, misunderstood you. Check out my update and get back to me if you still need help.

avatar image Henri 1 · Apr 06, 2011 at 06:24 AM 0
Share

Thanks Statement, but isn't that script to send a message from C#? I allready sent a message called "DoExplode" from Java, I only need to receive the message in C# and activate the required function. Any chance you could take a look at the following question as well? http://answers.unity3d.com/questions/50447/resize-cylinder-to-hit-distance-collision-detection-and-raycast-hit-distance-pr

avatar image Henri 1 · Dec 13, 2011 at 09:24 PM 1
Share

Hi @Statement, I eventually figured it out, and you were correct :blush with akward feeling:... I used in Java:"Send$$anonymous$$essage ("doExplode")" and in C# "void doExplode()"

Show more comments
avatar image
0

Answer by Valdemars Magone · Apr 08, 2011 at 09:44 AM

I'm trying to do the oposite thing - to send message from C# to Js. I have Player with javascript, which opens door in game with raycast, when player meets enemy, raycast lenght is set to 0, when he kills enemy, my enemy's C# script would destroy enemy gameObject and set players raycast back to 5. That would be a simple function in player's script :

function EnableRay () {
 rayCastLength += 5; 
}

My enemy script looks like this :

using UnityEngine; using System.Collections;

public class NewRobotLives : MonoBehaviour { //Fields public int lives = 3; public ParticleEmitter blood;

 //Trigger Function
 void OnTriggerEnter(Collider col)
 {
     if (col.tag == "bullet")
     {
         Debug.Log("Collision with bullet detected");

         //Decrease Life and destroy bullet
         lives -= 1;
         Destroy(col.gameObject);
     }

     //Check if we destroy our enemy
     if (DidEnemyDie())  {     
         Destroy(gameObject);
         Instantiate (blood, transform.position, transform.rotation);
         iTweenEvent.GetEvent(GameObject.FindWithTag("PlayerTag"), "Shake").Play();

      // here I should call EnableRay( )  in player' s script

     }


 }

 //Function return true if life < 0
 bool DidEnemyDie()
 {
     return lives <= 0 ? true : false;
 }

}

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 Statement · Dec 14, 2011 at 01:00 PM 0
Share

You'd do the same thing. player.Send$$anonymous$$essage("EnableRay");

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Help With Homing Missiles - Annoying! 3 Answers

How do i Specify a Receiver for a Send Message Function? 2 Answers

how to make one script change a variable in another scipt 2 Answers

Sending a message to script of another game object 3 Answers

SendMessage has no receiver 0 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