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 iamthecoolguy11 · Oct 10, 2013 at 11:13 PM · javascriptraycasthitsendmessage

RayCast to send message to the object it hits?

I have a zombie and its got simple AI on it and I have this gun script that should send a message to it. But it keeps saying "Object reference not set to an instance of an object". anyway here are the scripts.

GUN SCRIPT

 var hit : RaycastHit;
 var AmoInClip = 0;
 var Clips = 2;
 var AmoInClips = 30;
 var AmoLeft = 0;
 var BulletDammage = 100;
 var Bullet : Transform;
 var Spawn : GameObject;
 
 
 function Update()
 {
     var hit : RaycastHit;
     
     if(Input.GetMouseButtonDown(0))
     {
         Fire();
     }
     
     if(Input.GetKeyUp(KeyCode.R))
     {
     
         Reload();
     }
     
     if(AmoInClip <= 0)
     {
         
     }
     
     AmoLeft = AmoInClips * Clips + AmoInClip;
     
 }
 
 
 
 function Fire()
 {
     if(hit.gameObject.WithTag("zombie"))
     {
     
     hit.collider.SendMessageUpwards ("ApplyDammage", BulletDammage, SendMessageOptions.DontRequireReceiver);
     }
 }
 
 
 
 
 
 function Reload()
 {
     yield WaitForSeconds(2);
     AmoInClip = 0;
     Clips -= 1;
     AmoInClip = AmoInClips;
     
 }
 
 function OnGUI()
 {
     GUI.Label (Rect (0,80,80,20), AmoLeft.ToString(), "box");
 }



ZOMBIE SCRIPT

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var chaseRange = 15.0;
 var attackRange = 1.5;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 var attackRepeatTime = 1;
 var trigger : boolean = false;
 var TheDammage = 40;
 
 var DamageToMe = 0;
 
 private var attackTime : float;
 
 var controller : CharacterController;
 var gravity : float = 20.0;
 private var MoveDirection : Vector3 = Vector3.zero;
 
 function Start ()
 {
     DamageToMe += Random.Range(1,600);
     moveSpeed = Random.Range(2,5);
     Target = GameObject.FindWithTag("Player").transform;
     attackTime = Time.time;
 }
 
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         lookAt();
     }
     
     
     
     if (Distance < attackRange)
     {
         attack();
     }
     else if (Distance < chaseRange)
     {
         chase ();
     }
 }
 
 function lookAt ()
 {
     
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     //transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
      var dir = Target.position - transform.position;
      dir.y = 0; // kill height differences
      transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), moveSpeed * Time.deltaTime);
 
     
 }
 
 function chase ()
 {
     
     if(trigger == true)
     {
         transform.localPosition.y += 0.05;
     }
     
     
     moveDirection = transform.forward;
     moveDirection *= moveSpeed;
     
     
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
     
 }
 
 function attack ()
 {
     if (Time.time > attackTime)
     {
         
         Target.SendMessage("hurt");
         attackTime = Time.time + attackRepeatTime;
     }
 }
 
 function ApplyDammage ()
 {
     chaseRange += 30;
     moveSpeed += 2;
     lookAtDistance += 40;
 }
 
 function OnTriggerEnter()
 {
     
     trigger = true;
     yield WaitForSeconds(1);
     trigger = false;
 }
 
 function Hurt()
 {
     DamageToMe += 100;
     if(DamageToMe <= 0)
     {
         Destroy(gameObject);
     }
 }
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
2
Best Answer

Answer by aldonaletto · Oct 10, 2013 at 11:26 PM

The function Fire is wrong: you should do a Raycast in order to fill the RaycastHit structure - something like this:

 function Fire()
 {
   // do a raycast in the gun forward direction:
   if (Physics.Raycast(transform.position, transform.forward, hit){
     // if something is hit, check whether it's tagged "zombie":
     if(hit.gameObject.CompareTag("zombie"))
     { // if so, apply damage to it
       hit.collider.SendMessageUpwards ("ApplyDammage", BulletDammage, SendMessageOptions.DontRequireReceiver);
     }
   }
 }

But be aware that the zombie's ApplyDammage function isn't applying any damage at all - apparently, the damage is applied by the Hurt function; either use SendMessage("Hurt",...) or change the ApplyDamage code to call Hurt.

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 iamthecoolguy11 · Oct 11, 2013 at 12:15 AM 0
Share

thanks! but im getting a error with it

$$anonymous$$issingFieldException: UnityEngine.RaycastHit.gameObject

avatar image robertbu · Oct 11, 2013 at 12:29 AM 1
Share

Change line 6 to:

 if (hit.collider.tag == "zombie")

or:

 if (hit.collider.gameObject.CompareTag("zombie"))

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

16 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

Related Questions

Optimize script or Other way get material name (js) 2 Answers

Raycast with SendMessage can send Parameter, but how to send a variable ? 2 Answers

Detect Object that are hit by a ray 3 Answers

Findout if an object that was hit by raycast is still being hit (30 object that are instantiated) 0 Answers

Player wont be found on start 1 Answer


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