public class TopClass
{
[System.Serializable]
public struct WantToBeVisibleInInspector
{
public SomeEnum appearsInInspector;
public SomeClass doesNOTAppearInInspector;
}
[SerializeField] private WantToBeVisibleInInspector[] meWant;
}
SomeEnum shows on the inspector but the SomeClass does not.
Why does this occur and how to fix it?
Related
I need to call a protected variable from a public class into an if statement in a private method of another public class
I am programing a video game in unity and I need to use a bool variable (that shows if the character is out of stamina) in an if statement to determine whether or not the character can run
This is what my code looks like excluding everything unrelated to the problem
Public class CharacterStats : MonoBehaviour
{
[SerialzeField] protected bool Tired;
}
Public class PlayerMovement : MonoBehaviour
{
Private void HandleRunning()
{
If (Input.GetKeyDown(KeyCode.LeftShift) && X != True)
{
Speed = RunSpeed;
}
}
}
X is where I want the Tired variable to be.
Use a public readonly property like e.g.
public class CharacterStats : MonoBehaviour
{
// Keep you serialized field protected
[SerialzeField] protected bool tired;
// Have a public read-only accessor property
public bool Tired => tired;
}
and then e.g.
public class PlayerMovement : MonoBehaviour
{
// Somehow you will need to get a reference to the CharacterStats instance
// e.g. via the Inspector
[SerializeField] private CharacterStats stats;
[SerializeField] private float RunSpeed;
private float Speed;
private void HandleRunning()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && !stats.IsTired)
{
Speed = RunSpeed;
}
}
}
Alternatively (and my apologies to #aybe who had answered this) you can actually directly serialize a property using explicitely
[field: SerializeField] public bool Tired { get; protected set; }
this is a property which can be accessed by everyone but only this and inherited classes (and due to the serialization now the Inspector) have the permission to set the value.
In general: Fix your casing! In c# all keywords are lower key!
While studying interface I came along this weird behaviour
When I am running this
int num=20;
public void sound();
public void eat();
}
class Dog implements Animal{
public void sound(){
System.out.println("Wooof!!!!!!!");
}
public void eat(){
System.out.println("Food");
}
}
public class Main{
public static void main(String[]args){
Dog dog=new Dog();
dog.sound();
dog.eat();
System.out.println(Dog.num);
//System.out.println(Dog.num1);
}
}
It runs fine while If I declare a no static variable with same name i.e. num as of the one in interface like this
interface Animal{
int num=20;
public void sound();
public void eat();
}
class Dog implements Animal{
int num=10;
public void sound(){
System.out.println("Wooof!!!!!!!");
}
public void eat(){
System.out.println("Food");
}
}
public class Main{
public static void main(String[]args){
Dog dog=new Dog();
dog.sound();
dog.eat();
System.out.println(Dog.num);
//System.out.println(Dog.num1);
}
}
It gives this error Main.java:22: error: non-static variable num cannot be referenced from a static context
My question was since one from interface is static and is of class level why the child class i.e. Dog compilation fails when I declare a non static instance level variable.
The problem is your are trying to access Dog class into System.out.println(). Not the variable dog where the class has been initialized.
If variable num is not static is impossible to read it without initialize the class, so if you want to do Dog.num, that variable has to be static.
If you don't add the variable into Dog class, the value will be declared in the interface, but if the variable exists in the class, the compiler will try read that.
Well i have this Enums to be set on the inspector but i want to limite which enum will show depending on the Main Enum Selected to avoid using different enum than the desired
public MainSortEnum Sort;
public SecondaryTypeEnum1 Type1;
public SecondaryTypeEnum2 Type2;
public SecondaryTypeEnum3 Type3;
And the Enums
public enum MainSortEnum
{
First,
Second,
Thirth,
}
public enum SecondaryTypeEnum1
{
FirstType,
SecondType,
ThirthType,
}
public enum SecondaryTypeEnum2
{
FirstType,
SecondType,
ThirthType,
}
public enum SecondaryTypeEnum3
{
FirstType,
SecondType,
ThirthType,
}
So i just want to Be able on the inspector on this script to select the desired enum based on the MainSortEnum, is this posible?
You will need to create a custom inspector for the class that these enums are in.
For instance, we will call such a class MyClass:
... //other namespaces
using UnityEditor;
[CustomEditor(typeof(MyClass))]
public class MyClassEditor : Editor {
MyClass myClass;
void OnEnable() {
myClass = (MyClass)target;
}
public override void OnInspectorGUI() {
myClass.Sort = EditorGUILayout.EnumPopup("Sort", myClass.Sort);
if (myClass.Sort == MainSortEnum.First)
myClass.Type1 = EditorGUILayout.EnumPopup("Type 1", myClass.Type1);
else if (myClass.Sort == MainSortEnum.Second)
myClass.Type2 = EditorGUILayout.EnumPopup("Type 2", myClass.Type2);
else
myClass.Type3 = EditorGUILayout.EnumPopup("Type 3", myClass.Type3);
}
}
The easiest approach is to have multiple GameObjects (3 in your case) and switch the gameObject.SetActive(true/false) based of the user input.
This is my Example class and MonoBehaviour script:
using UnityEngine;
using System;
[Serializable]
public class TestSerializableClass
{
public int a;
public string b;
}
public class TestScript : MonoBehaviour
{
public TestSerializableClass field1;
[HideInInspector] public TestSerializableClass field2;
}
and this is the editor script for the MonoBehaviour Script
using UnityEditor;
[CustomEditor(typeof(TestScript))]
public class TestScriptEditor : Editor
{
SerializedProperty testClass;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
SerializedProperty testField = serializedObject.FindProperty("field2");
EditorGUILayout.PropertyField(testField, true);
}
}
This is how my inspector for TestScript looks.
Why doesn't it draw the "field2" varialble like the first one?
Can I do it with Editor script and property field? How?
(the includeChildre flag is true)
Remove the HideInInspector attribute from field2
Does anyone know how make complex UnityEvent appear in editor?
public class Test : MonoBehaviour
{
public UnityEvent myEvent;
public UnityEvent<string> myAnotherEvent;
}
myEvent appears just fine, but myAnotherEvent does not.
Thanks in advance.
UPD Also tried to override the UnityEvent like:
public class NewEvent : UnityEvent<string>
{
}
public class Test : MonoBehaviour
{
public UnityEvent myEvent;
public NewEvent myAnotherEvent;
}
didn't seem to help.
[System.Serializable]
public class NewEvent : UnityEvent<string>
{
}
public class Test : MonoBehaviour
{
public NewEvent myAnotherEvent;
}
You need to inform that your new class is serializable.