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 BertMDTV · Mar 25, 2015 at 08:00 AM · animationraycastvariablesaccessing scripts

Accessing Variables from Another Script

So I have script that is my raycast shooting script. Here is the script:

 var bulletTex : GameObject[];
 var Effect : Transform;
 var TheDammage = 100;
 var fireRate : float = 0.5;
 private var nextFire : float = 0.0;
 var gunShot : AudioClip;
 public var spreadFactor : float = 0.03;
 @HideInInspector
 public var spreadFactorOld : float = 0;
 var muzzleFlash : Renderer;
 var muzzleLight : Light;
 var gunShotAnimation : boolean;
 
 
 function Start() {
 
     muzzleFlash.enabled = false;
     muzzleLight.enabled = false;
 
 }
 
 function Update () {
 
     Fire();
 
 }
 
 function Fire () {
 
     if(Input.GetMouseButton(0) && Time.time > nextFire) {
      nextFire =Time.time + fireRate;
      ForceFire();
      AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
      
      }
 
 }
 
 function ForceFire () {
 
     var fwd = transform.TransformDirection(Vector3.forward);
     fwd.x += Random.Range(-spreadFactor, spreadFactor);
     fwd.y += Random.Range(-spreadFactor, spreadFactor);
     fwd.z += Random.Range(-spreadFactor, spreadFactor);
     var hit : RaycastHit;
     Debug.DrawRay(transform.position, fwd * 10, Color.green);
 
         
     if (Input.GetMouseButton(0))
     {
         if (Physics.Raycast (transform.position, fwd, hit, 100))
         {
             var bulletHole = Instantiate(bulletTex[Random.Range(0,1)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             Destroy(bulletHole.gameObject, 10);
             hit.transform.SendMessage("ApplyDammage", 10, SendMessageOptions.DontRequireReceiver);
             AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
             Shoot();
             gunShotAnimation = true;
             yield WaitForSeconds(0.01);
             gunShotAnimation = false;
         }
     }
 }
 
 function Shoot()
 {
     muzzleFlash.renderer.enabled = true;
     muzzleLight.enabled = true;
     yield WaitForSeconds(0.02);
     muzzleFlash.renderer.enabled = false;
     muzzleLight.enabled = false;
 }

At the top is the variable gunShotAnimation. When the raycast hits I have that variable become true and then after 0.01 seconds become false. The arms script is the script that is supposed to acces the gunShotAnimation variable. Here is the arms script:

 #pragma strict
 
 var AK47 : GameObject;
 
 internal var animator : Animator; //stores the animator component
 
 var armsRunning : boolean;
 var punchRight : boolean;
 var punchLeft : boolean;
 
 var AK47Idle : boolean;
 var AK47GunShot : boolean;
 
 var playerGunShot : RaycastShootingMachineGun;
 
 function Start () {
 
     animator = GetComponent(Animator); //assigns Animator component when we start the game
 
 }
 
 function Update () {
 
     Other();
 
 }
 
 function FixedUpdate () {
 
     animator.SetBool ("ArmsRunning", armsRunning);
     animator.SetBool ("PunchRight", punchRight);
     animator.SetBool ("PunchLeft", punchLeft);
     animator.SetBool ("AK47Idle", AK47Idle);
     animator.SetBool ("AK47GunShot", AK47GunShot);
 
 }
 
 function Other () {
     
     if (Input.GetKeyDown("left shift"))
     {
         armsRunning = true;
     }
     
     if (Input.GetKeyUp("left shift"))
     {
         armsRunning = false;
     }
     
     if (Input.GetMouseButtonDown(0))
     {
         punchLeft = true;
     }
     
     if (Input.GetMouseButtonUp(0))
     {
         punchLeft = false;    
     }
     
     if (Input.GetMouseButtonDown(1))
     {
         punchRight = true;
     }
     
     if (Input.GetMouseButtonUp(1))
     {
         punchRight = false;
     }
     
     if (AK47.active == true)
     {
         AK47Idle = true;
     }
     
     if (AK47.active == false)
     {
         AK47Idle = false;
     }
     
     if (playerGunShot.gunShotAnimation == true)
     {
         AK47GunShot = true;
     }
 
 }

So in the arms script at the top I call the raycast script with var playerGunShot : RaycastShootingMachineGun; Then at the bottom I say if the gunShotAnimation is true, then set AK47GunShot to true. When AK47GunShot is true then the AK47 gunshot animation will play.

When I hit play everything works except, of course, the gun shot animation. I get this error "NullReferenceException: Object reference not set to an instance of an object" on the line - if (playerGunShot.gunShotAnimation == true) - so basically it is saying that something doesn't exist. For some reason it isn't recognizing my gunShotAnimation variable.

I don't know why it is doing this. I could be writing it wrong but I don't know any other way to write the script. I think it's because I am writing it wrong so please if anybody can help me with this that would be great.

Thanks in advance!!!

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 Hrungdak · Mar 25, 2015 at 03:23 PM

You have to do a

 playerGunShot = GetComponent(RaycastShootingMachineGun) 

in the start method of the arms-script.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help With Animation Play Back - Reverse 1 Answer

How to make char animation idle 0 Answers

C# - Trigger Animation with Raycasting? 1 Answer

Can I animate objects/cameras in Update() without breaking Physics.Raycast? 1 Answer

Play Animation on Raycast *open and close some objects* 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