- Home /
Using raycast, find another object, send message.
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'. $$anonymous$$ake it capital d.
Yeah probably that's the problem :) you misspelled your world. Btw why do you use Send$$anonymous$$essage? why not just get the component and invoke it directly? But anyway yeah, you missed the spelling.
I thought Send$$anonymous$$essage's purpose is call method from other object. $$anonymous$$y 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.
Your understanding of Send$$anonymous$$essage 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.
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).
I changed function name "D" letter. Now its working :) But when I put many bombs, many Unity stucking. Stopped working and closing it for Task $$anonymous$$anager.
Answer by iwaldrop · Nov 22, 2013 at 07:10 AM
I bet it's because you're looping back and forth between two bombs. :)
When one bomb tells another to explode then the second will tell the first, which will tell the second, and on and on forever. You'll never leave the frame. You might even get a stack overflow.
You should have an isExploded flag, set it, and check it before doing anything.
private bool isExploded;
void DetonateNow()
{
if (isExploded)
return;
isExploded = true;
// the rest of the method as you have it.
}
But my code destroying object first. Destroy (this.gameObject)
That doesn't happen until the end of the frame. GameObject.Destroy.
"Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering."
error CS0126: An object of a type convertible to `bool' is required for the return statement
You must have given the method a return type though your code indicates it's void, and $$anonymous$$e definitely is void. You can also just forgot the return and execute the code if it's not exploded.
if (!isExploded)
{
isExploded = true;
// run code here
}
That's some kindergardener level code debugging. ;)
Your answer
Follow this Question
Related Questions
Layer Mask Detection 2 Answers
Struggling to set a RayCast from a camera object 0 Answers
Raycast and ScreenPointToRay 1 Answer
Raycast only works while moving 2 Answers
Rotate raycast towards mouse position 0 Answers