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
1
Question by sevensixtytwo · Oct 26, 2014 at 10:14 AM · javascriptvariableinheritanceweapons

How to check if variable exists?

Howdy. I've got a base Weapon class with two inheritors: SmallGun and LargeGun. The SmallGun fires in bursts and has a new variable called burstCount. The LargeGun doesn't.

In my scripts, how can I check if the variable burstCount exists in the current Weapon? Failing that, how would I check is the weapon is either a SmallGun or a LargeGun?

Thanks for the time.

Comment
Add comment · Show 5
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 Em3rgency · Oct 26, 2014 at 10:32 AM 1
Share

EDIT: convert to comment, OP wants JS.

C# objects have a GetType() method. Use it with typeof().

 weapon = new LargeGun();
 
 if(weapon.GetType() == typeof(SmallGun)){
     weapon.burstCount++; //bad practice. Should have a weapon.IncreaseBurstCount() method or smth
 }//else do nothing

avatar image sevensixtytwo Em3rgency · Oct 27, 2014 at 12:21 AM 0
Share

I appreciate the answer. $$anonymous$$ost of my code is in JS, tho, so I'll check around if there's an equivalent to GetType and typeof.

avatar image Em3rgency Em3rgency · Oct 27, 2014 at 03:28 PM 0
Share

You can specify either c# or js tags when posting questions :)

avatar image sevensixtytwo · Oct 28, 2014 at 12:51 AM 0
Share

$$anonymous$$y bad. Retagging.

avatar image Habitablaba · Oct 28, 2014 at 12:56 AM 0
Share

http://rockonflash.wordpress.com/2009/05/22/getting-object-type-in-unity3djavascript/

One quick google later...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zoodinger · Oct 28, 2014 at 04:37 AM

Look into Reflection. I don't know how much it is supported by JS, but it does whatever you are asking for. Keep in mind, however, that it's slow. Also, I would still advice that you find a better solution. Having to check if a variable exists is rarely ever a good idea.

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 sevensixtytwo · Oct 29, 2014 at 12:12 AM 0
Share

I see. So is there a way to check which child class it is then? Something like what Em3rgency posted but for JS?

 if weapon is SmallGun {
      getBurst
      }
avatar image Habitablaba · Oct 29, 2014 at 12:27 AM 0
Share

seriously? have you searched at all? check my comment above, or just click here: http://rockonflash.wordpress.com/2009/05/22/getting-object-type-in-unity3djavascript/

avatar image sevensixtytwo · Oct 29, 2014 at 02:17 AM 1
Share

yeeeah... well, I was hoping for something simpler and faster than getType. and yes, I did research on GetType before.

I've been experimenting on the "is" keyword for some time and it seems pretty close to what I need.

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

31 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 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

Change a Class in the inspector 1 Answer

Change static variable in JS through Csharp 1 Answer

HideInInspector with inherited variables 1 Answer

storing the value in a temporary variable problem 1 Answer

Is it possible to set variables for classes? 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