Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by murkantilism · Jul 26, 2015 at 10:19 PM · inspectorassignmentrequirecomponent

Require inspector assignment?

Was wondering if there was a way to require a variable be assigned via inspector, ala RequireComponent. I'm sure this has been asked plenty of times before, but all searches of Google and UA returned results about NullReferenceExceptions people were seeing because they forgot to assign something via inspector (which is exactly what I'm trying to prevent). I'm writing code that'll be used by others, including non-technical folks, so leaving a comment like

 // this var is assigned via inspector

doesn't help much.

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 zach-r-d · Jul 26, 2015 at 11:46 PM 0
Share

What do you want to happen if the inspector variable isn't assigned?

avatar image Hexer · Jul 26, 2015 at 11:50 PM 0
Share

You can say. If that var is null, secretly assign it to something so they won't notice it >:D.

But seriously if it is going to be used by others, then you atleast expect them to know the unity interface. They dont need to know code. Only how to change a public variable in the inspector.

But.. If it was not assigned you can call a Debug.LogError("Assign the var pls"); this will pauze unity.

avatar image murkantilism · Jul 27, 2015 at 12:05 AM 0
Share

The same thing as RequireComponent, don't let the user play or build until it's resolved

avatar image Gru · Mar 27, 2018 at 03:10 PM 0
Share

This is actually a really common use case. There are workflows with prefabs where previously assigned references would get unassigned by accident, and we wouldn't know until at some point in runtime where we would get the NullReferenceException on first use. This is unclear and problematic. I was expecting something like [RequireAssignedReference] attribute.

5 Replies

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

Answer by InfernoZYB · Jul 27, 2015 at 12:19 AM

You could do Hexers answer but if its not assign make it so the launcher stops playing.

 public GameObject ABC;
  
  if(ABC != null){
  //Line of code
  }
  else
  {
  Debug.LogError("assign the gameobject ABC in the inspector before resuming");
 UnityEditor.EditorApplication.isPlaying = false;
  }
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 murkantilism · Jul 27, 2015 at 01:18 AM 0
Share

Hmm this does seem like pretty much exactly what I'm looking for, though I wish there was a more elegant solution built into Unity but this works. Thanks!

avatar image Mikilo · Mar 27, 2018 at 04:12 PM 1
Share

I am late to the game, but even your answer is not good enough.

In latest Unity version you can do as followed:

 // Tell the Assert class to throw something on failure.
 UnityEngine.Assertions.Assert.raiseExceptions = true;
 // And check your fields.
 UnityEngine.Assertions.Assert.IsNotNull(variable, "$$anonymous$$ember \"variable\" is required.");
avatar image
0

Answer by Gru · Mar 27, 2018 at 03:45 PM

EDIT: Unity now knows to report the correct error in the Console, but does not stop play mode.

Your best bet would be to use the bailer idiom.

Extension method for convenience:

 public static partial class GameObject_ExtensionMethods {
     public static void UnassignedReference(this GameObject go, string referenceName) {
         Debug.LogError("Unassigned Inspector reference: " + referenceName, go);
 #if UNITY_EDITOR
         UnityEditor.EditorApplication.isPlaying = false;
 #endif
         }
 }

You must guard the last line, otherwise the project will not compile in Build.

 private void Start() {
     if(myVar == null) gameObject.UnassignedReference(nameof(myVar));
     // Normal Start Stuff...
 }

It is better to do these checks once in Start or Awake and proceed as normal than indent the code every time before use (bailer). The problem with this solution is the code will continue to run for the rest of Starts or Awakes, even though we set the isPlaying to false, and this will most probably cause other NullReferenceExceptions down the line, which could confuse the user.

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
avatar image
-1

Answer by Hexer · Jul 27, 2015 at 12:02 AM

 public GameObject ABC;
 
 if(ABC != null){
 //Line of code
 }
 else
 {
 Debug.LogError("assign the gameobject ABC in the inspector before resuming")
 }

This code will pauze unity if ABC were to be null.

Or do :

 If(ABC){
 //line of code
 }
 else
 {
     Debug.LogError("assign the gameobject ABC in the inspector before resuming")
 }

Either of one should work.

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 murkantilism · Jul 27, 2015 at 12:06 AM 0
Share

This is just checking if the variable is null, not emulating the behavior of RequireComponent

avatar image InfernoZYB · Jul 27, 2015 at 12:17 AM 0
Share

"This is just checking if the variable is null, not emulating the behavior of RequireComponent"

RequireComponent makes it so that there HAS to be a component attached to the gameobject the script is on. http://docs.unity3d.com/ScriptReference/RequireComponent.html

But i think i know what you are trying to get at.

avatar image murkantilism · Jul 27, 2015 at 01:18 AM 0
Share

Yes exactly, I'm asking about emulating that same behavior for inspector assignments

avatar image
0

Answer by undereyes · Mar 15, 2017 at 08:36 PM

Hi @murkantilism, my response comes a little late, but I just saw this, I'm sorry. You have a way to make this much more comfortable and automatic.

After the end of your Class add this code:

 [CustomEditor(typeof(*YOURCLASSNAME*), true)]
 public class *YOURCLASSNAME*Inspector : Editor
 {
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         *YOURCLASSNAME* myClass = (*YOURCLASSNAME*)target;
 
         if (myClass.yourVariable == null) myClass.yourVariable = yourValue;
     }
 }

Replace YOURCLASSNAME with your Class Name and yourVariable/yourValue with your variable/value.

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 murkantilism · Mar 15, 2017 at 10:01 PM 0
Share

If I understand this correctly, what this does is set the inspector variable to some default, "yourValue", if it's not assigned via the inspector. The original objective was to prevent the user from entering Play mode or running a build if there was a missing inspector assignment, just like RequireComponent does for components, rather than automatically applying a band-aid.

avatar image undereyes murkantilism · Mar 16, 2017 at 06:31 AM 1
Share

Yes, you have understood correctly. Sorry for the answer, I misunderstood it, too many hours working.

avatar image murkantilism undereyes · Mar 16, 2017 at 04:13 PM 0
Share

Haha no worries, thanks anyway for thoughts!

avatar image
0

Answer by Develax · Jun 01, 2019 at 05:15 PM

The helper class:

 using UnityEngine;
 
 internal static class MyDebug
 {
     public static void Assert<T>(T variable) where T : class
     {
         Debug.AssertFormat(variable != null, "The instance of '{0}' is `null`.", typeof(T).Name);
     }
 }

The usage:

 public class Entity : MonoBehaviour
 {
     public ExplosionDamage Damage;
 
     private void Awake()
     {
         MyDebug.Assert(Damage); // will print "The instance of 'ExplosionDamage ' is `null`."
     }
 }
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

29 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

Related Questions

Require Component not showing in inspeector 0 Answers

C# String Requires Manual Assignment in Inspector 1 Answer

When adding a script using GetComponent<>(), some prefabs that are assigned don't come up 1 Answer

Variable nos assigned on inspector, but it has been assigned on inspector ? 0 Answers

I am looking to add a class as a component to a prefab and have that class be a requiredcomponent of the script. Is this possible or am I wasting my time? 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