Hello, I’m trying do Bomberman game. My current problem is can’t send detonate message from one bomb to another bomb.
- player place bomb, which have some detonate range.
- player place other bomb, in range of first bomb’s detonate range.
- first bomb will detonate after 3 seconds.
- first bomb’s fire will hit second bomb and detonate it before it’s detonate time (3 sec).
I’m using Raycast for it. Current my bomb script:
using UnityEngine;
using System.Collections;
public class bomb : MonoBehaviour {
float detonateTime = 3f;
public GameObject explosionBomb;
public GameObject bombRay;
private int detonateLength = 2;
private RaycastHit hit;
void Start () {
StartCoroutine (destroyBomb());
}
IEnumerator destroyBomb() {
// code for detonate it after 3 sec
yield return new WaitForSeconds(detonateTime);
Destroy (this.gameObject);
Instantiate (explosionBomb, transform.position, Quaternion.identity);
// checking left direction for 3 range
// if there are no collider, which means it's empty air, make explosion
for (int i = 1; i <= detonateLength; i++) {
if (!Physics.Raycast(transform.position, Vector3.left, out hit, i)) {
Vector3 posBombRay = new Vector3(Mathf.Round(transform.position.x - i), transform.position.y, Mathf.Round (transform.position.z));
Instantiate(bombRay, posBombRay, Quaternion.identity);
} else if (hit.collider.tag == "Bomb") {
// if there are bomb left to him send detonate message to him
hit.collider.gameObject.SendMessage("DetonateNow");
}
}
}
void detonateNow() {
Debug.Log ("detonate now");
}
}
But error is:
SendMessage DetonateNow has no receiver!
UnityEngine.GameObject:SendMessage(String, Object)
<destroyBomb>c__Iterator0:MoveNext() (at Assets/Scripts/bomb.cs:43)
Your function is called 'detonateNow'. Make it capital d.
– AeonIxionYeah probably that's the problem :) you misspelled your world. Btw why do you use SendMessage? why not just get the component and invoke it directly? But anyway yeah, you missed the spelling.
– gajdotI thought SendMessage's purpose is call method from other object. My goal is catch object which is hit by first bomb, and access right that object (you know there are many bombs on screens), and call only that bomb's some function which detonates him.
– thisizmonsterYour understanding of SendMessage is correct, it's just the slower way to do what you're trying to do. Since you already have a reference to the object that you've just hit with your Raycast, then it's a trivial matter to grab the Component and invoke a method. See [here][1]. Bomb otherBomb = hit.collider.gameObject.GetComponent< Bomb >(); otherBomb. DetonateNow(); This assumes that you update your method name to starting with an uppercase (as is the C# convention). [1]: http://forum.unity3d.com/threads/37369-SendMessage-vs-GetComponent
– iwaldropI changed function name "D" letter. Now its working :) But when I put many bombs, many Unity stucking. Stopped working and closing it for Task Manager.
– thisizmonster