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 CG-DJ · Jul 21, 2013 at 09:42 PM · nullreferenceexceptionclassgunnull reference exceptionguns

Problems with Classes...i think

Yeah, so I'm just trying to grasp the concept of classes, so bear with me.

I have these lines of code in my scripts. I want to make a class of gun attributes called Gun. Then I want to make a variable of "guns" which is a Gun class. I can put as many different guns' attributes in the variable as I want. Then I will use a simple number variable to designate which one to use.

 class Gun
 {
     var fireRate : float; //Bullets per second
     var range : float;
     var damage : float;
     var clipSize : int;
     var maxBullets : int;
     var bulletsLeft : int;
     var bulletsStored : int;
 }
 
 var guns : Gun[];
 var currentGun : Gun = guns[currentGunNum];

The problem is I keep getting a "NullRefException: Object Reference not set to an instance to an instance of an object". I have filled up the "guns" variable with 2 elements of variables, but the currentGun variable is empty. I want currentGun to take on the attributes of what ever gun currentGunNum is.

If you guys need more of my script, you can find it here: https://dl.dropboxusercontent.com/u/33872519/PlayerBehavior.js

thanks for the help!

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 IgorAherne · Jul 21, 2013 at 09:57 PM

 var guns : Array = newArray();
 
 var _Gun1 : Gun = new Gun();
 
 guns.Push(_Gun1);


if you need to reference _ Gun1 from your guns do this:

 (guns[0] as Gun) 

_Gun1 belongs to the Gun class, and such variable will contain specified variables in it. For example, lets get the 0th cell from guns (_Gun1) and then get fireRate of the _Gun1.

 (guns[0] as Gun).fireRate = 12.0;

Всегда пожалуйста)

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 CG-DJ · Jul 22, 2013 at 02:43 PM 0
Share

Ok, but then how would I reference other guns? Will I have to write separate code for each weapon?

 if(Gun1 == true)
 {
    Gun1.fireRate = fireRate;
 }
 if(Gun2 == true)
 {
    Gun2.fireRate = fireRate;
 }

Sorry, new to arrays and classes. What I was trying to do was something like this: http://answers.unity3d.com/questions/202191/spawn-n-enemies-every-nth-wave.html

I like having an dropdown menu of several guns attributes and then being able to access the variables by their number.

thanks for helping a confused soul!

avatar image IgorAherne · Jul 22, 2013 at 02:58 PM 0
Share

guns is a "collection" of objects of class Gun.

But each gun object so far is just a collection of variables.

you can set the parameters of each gun in guns, like shown in the last line of my answer,

and you would be able to to plug in such value somewhere in Update, where the whole code would be.

Basically, Gun is like an array of 7 objects.

When we create an instance of class

var _Gun1 : Gun = new Gun();

we are creating an array-like thing. _Gun now holds 7 variables, and we can have many of _Gun objects in some kind of array, like guns

but _Gun is just a collection of variables which cary values at the moment.

You could also place a function in class Gun, and then every instance of such class would have a function, but so far u just have variables.

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

How Do I Make My Gun JUST Semi-AutoMatic? 3 Answers

Null Reference Exception after updating from 4.0 to 4.1.5 (coroutines) 0 Answers

Null reference exception after instantiating a light gameobject 0 Answers

Clicking out and then in to the Unity Player while game is running will something throw me a Null Exception. 2 Answers

NullReferenceException: Object reference not set to an instance of an object 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