- Home /
Public variable not showing in Inspector(Solved)
I had a quite simple script that kept count of the number of enemies in my scene and when that reached zero it loaded the next scene. Now I have multiple scenes I needed to change the variable number in the inspector for each scene rather than have a new script each time.
But for some reason the changing the variable to a public one has not let it show up in the inspector?
using UnityEngine;
using System.Collections;
public class LevelCleared : MonoBehaviour
{
public static int enemyNumbers = 10;
void Awake()
{
//enemyNumbers = 21;
}
void Update()
{
if(enemyNumbers <= 0)
{
var i = Application.loadedLevel;
AutoFade.LoadLevel(i+1 ,2,1,Color.black);
//Parameter 1 - LevelName or LevelIndex
//Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
//Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
//Parameter 4 - FadeColor - this is the color the screen fades to.
}
}
}
The variable was originally a static in, now public static. The commented out line in the awake function is just a left over from when there was a single scene.
I'm getting no compiler errors and the script still does what I need depending on what number I set the int variable to. It just doesn't show in the inspector so I have to manually change the script.
Any ideas
???
edit..
Script is attached to a sceneManager object in my scene and accessed from another script using -
LevelCleared.enemyNumbers -= 1;
???
Static variables dont show up in the inspector. You can assign it only as a public variable ans assign the public variable to another static variabe. Like,
public int enemyNumbers = 10; private static int staticEnemyNumber = enemyNumbers;
And then use staticEnemeyNumber throughout the script.
:)
Assu$$anonymous$$g I understood you, my script should now look like -
using UnityEngine;
using System.Collections;
public class LevelCleared : $$anonymous$$onoBehaviour
{
public int killCount = 10;
private static int enemyNumbers = killCount;
void Update()
{
if(enemyNumbers <= 0)
{
var i = Application.loadedLevel;
AutoFade.LoadLevel(i+1 ,2,1,Color.black);
//Parameter 1 - LevelName or LevelIndex
//Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
//Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
//Parameter 4 - FadeColor - this is the color the screen fades to.
}
}
}
But I,m getting compiler error -
Assets/LevelCleared.cs(8,43): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `LevelCleared.killCount'
???
You should initialize your static variable in a start or awake function to be sure that killCount exists.
void Awake(){
enemyNumbers = killCount;
}
using UnityEngine;
using System.Collections;
public class LevelCleared : $$anonymous$$onoBehaviour
{
public int killCount = 10;
private static int enemyNumbers = killCount;
void Awake()
{
enemyNumbers = killCount;
}
void Update()
{
if(enemyNumbers <= 0)
{
var i = Application.loadedLevel;
AutoFade.LoadLevel(i+1 ,2,1,Color.black);
//Parameter 1 - LevelName or LevelIndex
//Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
//Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
//Parameter 4 - FadeColor - this is the color the screen fades to.
}
}
}
Gives error -
Assets/LevelCleared.cs(8,43): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `LevelCleared.killCount'
well the error is not in this cript, put your static variable to public and that should do it.
Answer by Digital-Phantom · Apr 08, 2015 at 08:16 AM
ok problem solved (although I'll admit I don't understand why...lol)
using UnityEngine;
using System.Collections;
public class LevelCleared : MonoBehaviour
{
public int killCount = 10;
public static int enemyNumbers;
void Awake()
{
enemyNumbers = killCount;
}
void Update()
{
if(enemyNumbers <= 0)
{
var i = Application.loadedLevel;
AutoFade.LoadLevel(i+1 ,2,1,Color.black);
//Parameter 1 - LevelName or LevelIndex
//Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
//Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
//Parameter 4 - FadeColor - this is the color the screen fades to.
}
}
}
Changing the enemyNumbers to a public static in (in conjunction with the killCount variable) works fine. I get to edit the value in the inspector and still access the script from other scripts.
Seems like a waste of time to me, I'm still able to influence a static variable from the inspector, just takes another few lines of code to do it. I mean... why not just make it possible to have a public static int in the first place.
Anyway, all sorted. Thanks for the help guys
:)
I think you misunderstand the meaning of static my friend, it has nothing to do with accessibility, other then that you can't edit a static member on the inspector because, well it's static.
You can't just access variables by setting them to public, you still need a reference to what instance of the class you want the value from.
The only reason you can access a static variable is because it's a static value that is the same for all instances of the class.
The way you are doing here is NOT correct, and WILL give you problems in the future.
@winsjansen Can you please elaborate on the correct way then?
Your answer
Follow this Question
Related Questions
Is it possible to show Static Variables in the Inspector? 10 Answers
How to set a public variable to a gameObject in the inspector through coding 1 Answer
Can I use strings with static variables? 2 Answers
Whats the difference between public and static variable? 2 Answers
Avoid public variable to get overwritten by the Inspector 1 Answer