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 plsresbond · Jan 22, 2013 at 09:43 PM · fpsnullreferenceexceptionsway

Why won't this weapon sway script work?

I'm trying to get this script to work, but it says "NullReferenceException: Object reference not set to the instance of an object" On line 16. Please help, and thanks ahead of time!

 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 · Show 4
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 cdrandin · Jan 22, 2013 at 10:15 PM 0
Share

From counting down line 16 is DefaultPos, but that doesn't seem right. Which one is it? Also, it is convention that all declared variables should start lowercase then every other word after be upper case, I don't remember the na$$anonymous$$g standard.

avatar image plsresbond · Jan 22, 2013 at 10:19 PM 0
Share

It is the line: DefaultPos = transform.localposition;

But I don't know what's wrong with it, I'm new to this, can anyone help?

avatar image cdrandin · Jan 22, 2013 at 10:26 PM 0
Share

lol, uppercase P. DefaultPos = transform.localPosition; These bite me in the ass all the time. Always make sure to refer to the documentation to double check syntax and definition.

avatar image plsresbond · Jan 22, 2013 at 10:31 PM 0
Share

Thanks! Well, it got rid of the error, but the script still isn't making any effect at all... Any ideas? I've been so frustrated over this script, I'm so lost...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 24, 2013 at 05:59 AM

One of your problems is how you are using Lerp(). The final parameter of Lerp() uses a value between 0 and 1. Larger values are truncated so only the fractional part is used. For most uses, this value needs to increase or decrease over time. Time.deltaTime, since it is the time since the last frame, will not steadily increase or decrease.Change Time.deltaTime to Time.time;

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

11 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

Related Questions

NullReferenceException on my gunscript 1 Answer

FPS gun movement 3 Answers

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

Converting to 3rd Person from 1st Person - Island Demo 1 Answer

Vector3.Lerp and Animations 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