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 thisizmonster · Nov 21, 2013 at 08:11 PM · raycastingraycasthitsendmessage

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.

  1. player place bomb, which have some detonate range.

  2. player place other bomb, in range of first bomb's detonate range.

  3. first bomb will detonate after 3 seconds.

  4. 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)
Comment
Add comment · Show 9
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 AeonIxion · Nov 21, 2013 at 09:30 PM 0
Share

Your function is called 'detonateNow'. $$anonymous$$ake it capital d.

avatar image gajdot · Nov 22, 2013 at 04:17 AM 1
Share

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.

avatar image thisizmonster · Nov 22, 2013 at 06:47 AM 0
Share

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.

avatar image iwaldrop · Nov 22, 2013 at 07:00 AM 0
Share

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).

avatar image thisizmonster · Nov 22, 2013 at 07:08 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.
 }
Comment
Add comment · Show 10 · 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 thisizmonster · Nov 22, 2013 at 07:53 AM 0
Share

But my code destroying object first. Destroy (this.gameObject)

avatar image iwaldrop · Nov 22, 2013 at 07:56 AM 0
Share

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."

avatar image thisizmonster · Nov 22, 2013 at 11:09 AM 0
Share

i see, ty, i'll try

avatar image thisizmonster · Nov 24, 2013 at 08:19 AM 0
Share

error CS0126: An object of a type convertible to `bool' is required for the return statement

avatar image iwaldrop · Nov 24, 2013 at 09:05 AM 0
Share

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. ;)

Show more comments

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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