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 Omti1990 · Dec 07, 2020 at 06:30 PM · staticpropertiesnull reference

Is there some way to make a property return a static value if the object is null/does not exist?

Hello,
can I make a property of an object return a static value if the object itself is null? I'm trying to use a component system for some of my game objects and I figured it'd be convenient if the property just returned the value 0 if the object didn't exist (if my spaceship didn't have a booster, I don't want to check for missing boosters all the time) It'd be far more convenient if booster power requests just returned 0. My current attempt at coding this looked like this, but it returns null reference errors if I actually try to access the properties.

 private bool initialised = false;
     [SerializeField, Tooltip("maximum boosting Time for BoosterDrive ScriptableObject"), Range(0f,1000f)] private float maxBoost = 100f;
     public float MaxBoost
     {
         get
         {
             if(this == null)
             {
                 return 0f;
             }
             else return maxBoost;
         }
     }


Honestly no clue if this is even possible, but it'd be very neat if it was.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Dec 07, 2020 at 07:17 PM

I believe you can use Null Conditional Operators and Null coalesciong operators to accomplish this. Something like

 public static float GetMaxBoost(BoosterDrive boost)
      {
          return boost?.maxDrive ?? 0f;
      }

Which basically checks if boost in null before it tries to access its property, and if it is null then it returns 0 instead. I haven't really used these myself and I haven't tested this code so I'm not sure if it works.

Comment
Add comment · Show 7 · 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 unity_ek98vnTRplGj8Q · Dec 07, 2020 at 07:19 PM 0
Share

Of course this does not have to be a static method, you could use it from the calling function like this

 Boost boost = null;
 float maxBoost = boost?.maxBoost ?? 0f;
avatar image sacredgeometry unity_ek98vnTRplGj8Q · Dec 07, 2020 at 07:25 PM 0
Share

I think you are missing the point. Go read his original post :)

avatar image sacredgeometry · Dec 07, 2020 at 07:20 PM 0
Share

That isn't really what he asked though. Yes null coalescing and propagation does what they say they do. But trying to access instance members from a static method is not going to work unless you are using a singleton pattern (or any similar) which literally keeps a static reference to an instance of the whole object.

avatar image Omti1990 sacredgeometry · Dec 07, 2020 at 07:34 PM 1
Share

Not using a singleton pattern.

I was just trying to look for a way to optimise my code, but it seems my big brain idea just doesn't work.

avatar image sacredgeometry Omti1990 · Dec 07, 2020 at 07:36 PM 0
Share

K.I.S.S ... keep it simple ... stupid ;). The biggest brain move is being able to make something work in the most simple way. Don't try to complicate it its probably a sign that you are doing something wrong.

avatar image unity_ek98vnTRplGj8Q sacredgeometry · Dec 07, 2020 at 07:37 PM 0
Share

If I'm understanding correctly, he wants to be able to say

 float maxBoost = boost.maxBoost;

And have it equal 0 if boost is null. You correctly answered that no, this is not possible as maxBoost is a property of an instance of boost. However, it seems like he wants his code to be as close to this as possible, which is why I replied with the answer that I did.

 float maxBoost = boost?.maxBoost ?? 0f;

If he does not want to use my answer then that's fine, but I don't think I've given an incorrect answer.

avatar image sacredgeometry · Dec 07, 2020 at 07:24 PM 0
Share

Similarly trying to check if this is null in a instance properties getter is not going to give you much in the way of joy either. Read up on null references/ pointers and you will see why.


https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null#:~:text=null%20(C%23%20Reference)&text=The%20null%20keyword%20is%20a,except%20for%20nullable%20value%20types.

avatar image
1

Answer by sacredgeometry · Dec 07, 2020 at 06:45 PM

If this is null then you can't access properties on an object because its a null pointer i.e. it doesn't have a type of your object so it doesnt have the properties of your object either.


i.e. you cant use instance members if you dont have an instance to reference.

Comment
Add comment · Show 6 · 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 Omti1990 · Dec 07, 2020 at 07:02 PM 0
Share

Thank you. Do you know if there's some way to make a static function return something non-static?

Or do I just have to deal with it and do something like this?

 public static float Get$$anonymous$$axBoost(BoosterDrive boost)
     {
         if(boost == null) return 0f;
         else return boost.$$anonymous$$axBoost;
     }

using this as a property:

private float $$anonymous$$axBoost { get {return maxBoost;}}

Just put that to private since I wouldn't want anyone accessing it from the outside. (Is this possible anyways?)

avatar image sacredgeometry Omti1990 · Dec 07, 2020 at 07:13 PM 1
Share

You can make a static method return what ever you want you just need a reference to the object if it doesnt have one.

Static just "means" class (at least you can think of it like that) in this context ... i.e. it's a class method. So loosely speaking there is no concept of this because there is no instance for this to apply to. When you make a static member it belongs to the class e.g.

 public class $$anonymous$$yClass
 {
     public static int $$anonymous$$yInt = 120;
 }



There is only one $$anonymous$$yInt ... it is not on the instance its on the class. So if you did

 var a = new $$anonymous$$yClass();
 
 a.$$anonymous$$yInt = 23;


You would get an error because its static ... i.e. $$anonymous$$yClass.$$anonymous$$yInt. Similarly if you instantiated a bunch of $$anonymous$$yClass objects and changed $$anonymous$$yInt it would "change for all of them" because it doesnt live on the instance :D.

avatar image sacredgeometry Omti1990 · Dec 07, 2020 at 07:15 PM 0
Share

I think you are getting tied up in a knot. What are you actually trying to achieve because there is probably a much simpler way to do it.

avatar image Omti1990 sacredgeometry · Dec 07, 2020 at 07:30 PM 0
Share

I think unity_ek...'s idea is what I basically want.

The idea was that basically when I as a player try to use boost it's just using the property of the instance of the scriptable object. If I haven't actually put a booster object on my spaceship it should just add zero boost.

I just figured this might be a way to keep the lenght of the class that actually uses the boost shorter.

 void AddBoost(BoosterDrive boost)
 {
     accelerationVector = accelerationVector + accelerationVector.normalized * boost?.maxBoost ?? 0f;
 
 }
Show more comments

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

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

pre generated static Object in C# 1 Answer

How to list all the properties and methods from the class? 1 Answer

Using static properties that return _instance.variable... 0 Answers

Water4 with different properties across different scenes 0 Answers

how to make easily accessible classes. the type like Unity has in JS for example. 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