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 BHS · Jun 10, 2012 at 12:06 AM · gunsway

Gun Sway Script Not Working Properly on Start

Hi,

I found this helpful gun sway script on the here that was posted as an answer, but there's one issue I have been trying to fix with it for hours. On start up I get:

    NullReferenceException: Object reference not set to an instance of an object

It has to do with the function start on the script below, but it's getting the local position so I don't know why it errors. If I play past the error by unpausing it, it works perfectly. I'm very confused by this, does anyone know why?

Here's the code:

 public var MoveAmount : float = 1;

 public var MoveSpeed : float = 2;

 public var GUN: GameObject;

 public var MoveOnX : float;

 public var MoveOnY : float;

 public var DefaultPos : Vector3;

 public var NewGunPos : Vector3;

 function Start(){

 DefaultPos = transform.localposition;   

 }

 function Update () {

 MoveOnX = Input.GetAxis("Mouse X") * Time.deltaTime * MoveAmount;

 MoveOnY = Input.GetAxis("Mouse Y") * Time.deltaTime * MoveAmount;

 NewGunPos = new Vector3 (DefaultPos.x+MoveOnX, DefaultPos.y+MoveOnY, DefaultPos.z);

 GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition, NewGunPos, 
 MoveSpeed*Time.deltaTime);

   }
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 Mortoc · Jun 10, 2012 at 01:07 AM

The only reference in your script that can be null is the GUN object. Verify that you have set the reference in the editor.

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 BHS · Jun 10, 2012 at 01:18 AM 0
Share

By reference do mean actually set the object to the gameobject spot in the script? Yes, this has been done. I even removed it and set this directly on the object so I wouldn't need the gameobject variable and I still got the error. Could this be a parent or child problem? What am I doing wrong? Everything should be working fine.

avatar image BHS · Jun 10, 2012 at 01:30 AM 0
Share

So I tested the gun sway script on different objects and I got the same error with every object I set it to whether it was parented or a child. No madder what I put this script to I get the error. So, there's something logically incorrect with the script.

avatar image Weirdo-Studios · Jul 03, 2013 at 05:49 PM 0
Share

I have exactly the same issue know this is old post but cant find solution anywhere if anyone knows please help.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Gun Sway Script glitching 0 Answers

Problem with quaternion weapon sway 1 Answer

Gun sways way too much 0 Answers

Aim Down Sights and Weapon Sway not working unless animations are not assigned 0 Answers

How do i make my weapon sway? 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