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 manueldel99 · Jan 17, 2014 at 05:45 PM · physicsraycastfpsshoot

Shooting script no working correctly

Here's the script:

 var shotSound: AudioClip; // drag a shot sound here, if any
 var bloodPrefab: GameObject; // drag the blood prefab here, if any
 var sparksPrefab: GameObject; // drag the sparks prefab here, if any
 var effect : GameObject;
 var waterPrefab : GameObject;
 
 function Shoot(){
     if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
     var hit: RaycastHit;
     if (Physics.Raycast(transform.position, transform.forward, hit)){
         // prepare rotation to instantiate blood or sparks
         var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
         if (hit.transform.tag == "Enemy"){ // if enemy hit...
             if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
             hit.collider.gameObject.GetComponent(Animator).SetBool("Dying", true);
             hit.Destroy(CharacterController);
         } // and consume its health
         if (hit.transform.tag == "Water"){
             Instantiate(waterPrefab, hit.point, rot);
         }
         else { // otherwise emit sparks at the hit point
             if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
         }
     }
 }
 
 function Update(){
     if (Input.GetButtonDown("Fire2")){
         Shoot();
         var fx : GameObject = Instantiate(
            effect, transform.position, transform.rotation );
     }
 }


When I shoot the enemy, this error message pops up:

 MissingMethodException: UnityEngine.RaycastHit.Destroy
 Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
 Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
 Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
 Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args)
 Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey15.<>m__9 ()
 Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
 UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
 RayCast.Shoot () (at Assets/Standard Assets/Tutorial de shooter/RayCast.js:16)
 RayCast.Update () (at Assets/Standard Assets/Tutorial de shooter/RayCast.js:29)



I know the problem is in this line

 hit.Destroy(CharacterController);

but I don't know how to solve it. Please can anybody help me???

PD: Sorry for my bad English

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 17, 2014 at 05:48 PM

You are trying to destroy the character controller attached to this specific game object? Try this:

 Destroy(hit.collider.GetComponent(CharacterController));

If for some reason you have trouble with this, you can do:

 hit.collider.GetComponent(CharacterConroller).enabled = false;

This will disable the character controller, not destroy it.

Comment
Add comment · Show 3 · 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 manueldel99 · Jan 17, 2014 at 08:18 PM 0
Share

How do I check if the enemy has a character cotroller from the script? I get this error:

 $$anonymous$$issingComponentException: There is no 'CharacterController' attached to the "GameObject" game object, but a script is trying to access it.
 You probably need to add a CharacterController to the game object "GameObject". Or your script needs to check if the component is attached before using it.
 RayCast right.Shoot () (at Assets/Standard Assets/Tutorial de shooter/RayCast right.js:16)
 RayCast right.Update () (at Assets/Standard Assets/Tutorial de shooter/RayCast right.js:29)


I've tried both thing you said

avatar image robertbu · Jan 17, 2014 at 08:44 PM 1
Share

Now I understand what you were trying to do. I edited my answer. If you are not sure if the enemy has a CharacterController, you can do:

 var cc = hit.collider.GetComponent(CharacterConroller);
 if (cc != null)
     Destroy(cc);
avatar image manueldel99 · Jan 18, 2014 at 12:51 PM 0
Share

Thanks you!!

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

18 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

Related Questions

FPS PISTOL 1 Answer

How do I make a gun project a particle? 1 Answer

Where can i find an easy raycast shooting tutorial for C#? 2 Answers

Shoot Projectile 0 Answers

Raycast Positioning 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