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 Lucas · Oct 25, 2010 at 09:39 AM · raycastcolliderrendererlinegizmo

Need help fixing an error. Using raycast to mimic laster beam but no collider, Line Renderer, Raycast Collider.

I want to build a laser beam tower to attack the villains. If there was only one laser tower, it was straightforward. However, I can't hurt my enemy, I had Line Renderer in my object, do i need to add a raycast collider?

I wrote code like this "if(hit.collider.gameObject.tag == "LaserProjectile")" in my enemy's code.

Laser Code :

 var hit : RaycastHit; 
 Physics.Raycast(transform.position, transform.forward, hit , attackingRange,Space.World); 
 if (hit.collider){ 
     lineRenderer.SetPosition(1,Vector3(0,0,hit.distance)); 
 }else{ 
     lineRenderer.SetPosition(1, Vector3(0,0, attackingRange)); 
 } 

even though I can add "Destroy(hit.gameObject);" in the Laser code, nothing happen....

I really appreciate your help~

Comment
Add comment · Show 6
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 Jesse Anders · Oct 25, 2010 at 12:01 PM 2
Share

Gizmos are just for editing/debugging; they're not intended for in-game use. You'll probably want to use something like LineRenderer ins$$anonymous$$d. As for the exception you mention, please indicate which line of code generates the error; that'll make it easier for us to help. (I see some potential logic errors in your code, but I'm not sure if they're directly related to the problem.)

avatar image Lucas · Oct 25, 2010 at 01:57 PM 0
Share

Thanks for your comment, the problem is "if(hit != null && hit.collider.gameObject.tag == "Enemy") Destroy(hit.collider.gameObject);" , if I delete this two sentenses, no error occur, but no enemy was attacked. BTW, thanks for your recommend, I will try LineRenderer, Thank you $$anonymous$$r. Jesse.

avatar image spinaljack · Oct 31, 2010 at 02:17 PM 0
Share

$$anonymous$$aybe you've destroyed the enemy's collider if it's a child of the root game object ins$$anonymous$$d of the whole thing. Generally with tower defence games you'd want to use gameObject.Send$$anonymous$$essage("ApplyDamage",10); ins$$anonymous$$d to deal damage to the enemy ins$$anonymous$$d of removing it. (you need a function called ApplyDamage() on the enemy for this to work)

avatar image Lucas · Oct 31, 2010 at 03:00 PM 0
Share

Thanks spinaljack. But the problem is I can get (hit.collider) in Laser.js only, how to Send$$anonymous$$essage to Enemy.js? how to make sure which enemy was hurted? Thanks for your help..

avatar image Lucas · Oct 31, 2010 at 03:20 PM 0
Share

I read your question, and I tried to use Getcomponent to control enemies' HP, "if(hit.collider.gameObject.tag == "Enemy") hit.collider.gameObject.GetComponent("$$anonymous$$oving.js").HP -= 10;" However, the error is "HP is not a member of UnityEngine.Component".. Please help me $$anonymous$$OTORO..

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by denewbie · Nov 06, 2010 at 02:54 PM

if(hit != null && hit.collider.gameObject.tag == "Enemy") <--- if the hit was null the second part will have a null error so you have to split the 2 to either

if (hit != null) if (hit.collider.gameObject.tag == "Enemy") Destroy(hit.collider.gameObject);

This way you check if the hit was null first before you check for the collier.gameObject so you wont get the error.

Comment
Add comment · Show 2 · 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 Loius · Nov 06, 2010 at 04:35 PM 0
Share

No, if statements leave early when something fails. It needs to be written as

if ( hit && hit.gameObject && hit.gameObject.tag == "Enemy" )

though I'm pretty sure hit can't be non-null while hit.gameObject is null.

avatar image Lucas · Nov 07, 2010 at 05:33 PM 0
Share

Thank you guys. Then I use GetComponent to decrease HP of villans. var target : $$anonymous$$oving = hit.collider.gameObject.GetComponent("$$anonymous$$oving"); target_BTS.ApplyDamage (); this seems to work fine.

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

No one has followed this question yet.

Related Questions

JS Script only work in Editor 0 Answers

Line Renderer and Collider Question 0 Answers

C# Calculate Edge Collider for Line Renderer 1 Answer

Printing Distance b/w two points using line renderer and Raycast 1 Answer

Changing texture with raycast on iPad 2 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