// 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");
}
}
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;
}
}