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 /
This question was closed Jan 09, 2015 at 11:29 AM by Graham-Dunnett for the following reason:

Duplicate Question

avatar image
0
Question by yvyv1000 · Jan 09, 2015 at 11:28 AM · errorraycastnullreferenceexceptionshooting

raycast shooting

hello guys i have a script with a raycast shooting part in it but every time i use it it gives the error: NullReferenceException: Object reference not set to an instance of an object MoveTank.Update () (at Assets/P3J/Demo Scene/MoveTank.js:129)

my script:

 #pragma strict
 
 var leftTrack : MoveTrack;
 var rightTrack : MoveTrack;
 
 var acceleration : float = 5;
 
 var currentVelocity : float = 0;
 var maxSpeed : float = 25;
 
 var rotationSpeed : float = 30;
 
 var spawnPoint : Transform;
 var bulletObject : GameObject;
 var fireEffect : GameObject;
 var maxShots :  int  =  1 ;
 var shots :  int  =  0 ;
 public var ReloadTime:float = 2;
 var MaxAmmo : int = 40 ;
 
 var gunSnd : AudioClip;
 var Effect : Transform;
 var TheDammage = 100;
 
 function Start() {
 
     // Get Track Controls
     leftTrack = GameObject.Find(gameObject.name + "/Lefttrack").GetComponent(MoveTrack);
     rightTrack = GameObject.Find(gameObject.name + "/Righttrack").GetComponent(MoveTrack);
     
 }
 
 
 function Update () {
     
     if (Input.GetKey (KeyCode.W)) {
         // plus speed
         if (currentVelocity <= maxSpeed) 
             currentVelocity += acceleration * Time.deltaTime;
 
     } else if (Input.GetKey (KeyCode.S)) {
         // minus speed
         if (currentVelocity >= -maxSpeed) 
             currentVelocity -= acceleration * Time.deltaTime;
         
     } else {
         // No key input. 
         if (currentVelocity > 0) 
             currentVelocity -= acceleration * Time.deltaTime;
         else if (currentVelocity < 0) 
             currentVelocity += acceleration * Time.deltaTime;
 
     }
 
 
     // Turn off engine if currentVelocity is too small. 
     if (Mathf.Abs(currentVelocity) <= 0.05)
         currentVelocity = 0;
 
     // Move Tank by currentVelocity
     transform.Translate(Vector3(0, 0, currentVelocity * Time.deltaTime));
 
     // Move Tracks by currentVelocity     
     if (currentVelocity > 0) {
         // Move forward
         leftTrack.speed = currentVelocity;
         leftTrack.GearStatus = 1;
         rightTrack.speed = currentVelocity;
         rightTrack.GearStatus = 1;
     }
     else if (currentVelocity < 0)    {
         // Move Backward
         leftTrack.speed = -currentVelocity;
         leftTrack.GearStatus = 2;
         rightTrack.speed = -currentVelocity;
         rightTrack.GearStatus = 2;
     }
     else {
         // No Move
         leftTrack.GearStatus = 0;    
         rightTrack.GearStatus = 0;        
     }
 
 
     // Turn Tank
     if (Input.GetKey (KeyCode.A)) {
         if (Input.GetKey(KeyCode.DownArrow)) {
             // Turn right
             transform.Rotate(Vector3(0, rotationSpeed * Time.deltaTime, 0));
             
             leftTrack.speed = rotationSpeed;
             leftTrack.GearStatus = 1;
             rightTrack.speed = rotationSpeed;
             rightTrack.GearStatus = 2;
             
         } else {
             // Turn left
             transform.Rotate(Vector3(0, -rotationSpeed * Time.deltaTime, 0));
             
             leftTrack.speed = rotationSpeed;
             leftTrack.GearStatus = 2;
             rightTrack.speed = rotationSpeed;
             rightTrack.GearStatus = 1;
             
         }
     }
 
     if (Input.GetKey (KeyCode.D)) {
         if (Input.GetKey(KeyCode.DownArrow)) {
             // Turn left
             transform.Rotate(Vector3(0, -rotationSpeed * Time.deltaTime, 0));
             leftTrack.speed = rotationSpeed;
             leftTrack.GearStatus = 2;
             rightTrack.speed = rotationSpeed;
             rightTrack.GearStatus = 1;
 
         } else {
             // Turn right
             transform.Rotate(Vector3(0, rotationSpeed * Time.deltaTime, 0));
             leftTrack.speed = rotationSpeed;
             leftTrack.GearStatus = 1;
             rightTrack.speed = rotationSpeed;
             rightTrack.GearStatus = 2;
             
         }
     }
     
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));    
     
     // Fire!
     if (Input.GetButtonDown("Fire1") && shots < maxShots && MaxAmmo > 0) {
         if (Physics.Raycast (ray, hit, 100))
         {
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         }
         shots ++;
          Reload();
          MaxAmmo --;
     }
 }
 
 function Reload(){
  yield WaitForSeconds(ReloadTime);///Wait 2(ReloadTime) seconds before continuing
  shots = 0;
  
  }
  
  function OnGUI () {
     GUI.Label (Rect (750, 25, 100, 30), MaxAmmo.ToString() + " Shells left");
     GUI.Label (Rect (750, 50, 150, 30), ReloadTime.ToString() + " Sec reload time");
 }

Comment
Comments Locked
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

(2D) Raycast check - null reference even when (RayHit != null) 2 Answers

im having trouble with raycast shooting script 0 Answers

Ignore NullReferenceException error? 2 Answers

Object reference not set to an instance of an object help (JS) 0 Answers

Null Reference Expantion 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