- Home /
(SOLVED) [System.Serializable] / Inspector is not working
So: I am using unity 2021.1.16f. I am currently buidling a scene with some particesystems and stumbled across the problem not beeing able to easily detect a particles death or birth, to play sound on it. Now I have tried using a technique of counting how many particles there are and if its not there anymore is makes a sound, however that worked almost, since the sounds sometimes just did not play and sometimes cut off. Anyways, I found an article (https://everfounders.com/unity-particle-system/) by a Person giving a full tutorial about how to achieve that with tricks and techniques I yet have learned. The problem is just, that the code is outdated, for example I have to write [System.Serializable] instead of only [Serializable]. And that is exactly where my real problem begins:
I am trying to reproduce this code:
[Serializable]
public class ParticlesInfo
{
public ParticleSystem system;
public AudioClip sound;
public bool callEventsOnce;
public ParticlesEvent onBirth;
public ParticlesEvent onDeath;
[HideInInspector]
public float[] m_times;
[HideInInspector]
public ParticleSystem.Particle[] m_particles;
[HideInInspector]
public bool birthInvoked;
[HideInInspector]
public bool deathInvoked;
}
[Serializable]
public class ParticlesEvent : UnityEvent<ParticlesInfo>
{
}
I changed Serializable to System.Serializable and UnityEvent<ParticlesInfo> to UnityEvent :
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PacrticleEvents : MonoBehaviour
{
}
[System.Serializable]
public class ParticlesInfo
{
public ParticleSystem system;
public AudioClip sound;
public bool callEventsOnce;
public ParticlesEvent onBirth;
public ParticlesEvent onDeath;
//[HideInInspector]
public float[] m_times;
//[HideInInspector]
public ParticleSystem.Particle[] m_particles;
//[HideInInspector]
public bool birthInvoked;
//[HideInInspector]
public bool deathInvoked;
}
[System.Serializable]
public class ParticlesEvent : UnityEvent <ParticlesInfo>
{
public ParticlesInfo[] Particles;
}
What, so it says in the article, should show is this:
However [System.Serializable] not work. I have no ability at all, to see or change variables or other stuff in the Inspector. I don't know what I have done wrong. I did try to change my base class of ParticlesInfo to Monobehavior, but that didn't do anything at all.
So I am really Stuck and I wanna finish this until New Year, so I really hope someone can helpme with this.
Also already Thanks to everyone who will answer to this post, because that is definitly something.
Answer by Casiell · Dec 28, 2021 at 06:10 PM
Unity is serializing fields inside of ParticleEvents class. And your ParticleEvents class is empty. So you need to add fields to that class
What System.Serializable is doing, is telling Unity that if there ever is a field of that type in a class then Unity should display it in inspector. And since there are no fields, nothing is displayed
Well, I now have put SerializeFields all over my Code, to see if anything would show up in the Inspector:
[System.Serializable]
public class ParticlesInfo
{
[SerializeField] public ParticleSystem system;
public AudioClip sound;
public bool callEventsOnce;
[SerializeField]
public ParticlesEvent onBirth;
public ParticlesEvent onDeath;
//[HideInInspector]
public float[] m_times;
[SerializeField]
//[HideInInspector]
public ParticleSystem.Particle[] m_particles;
//[HideInInspector]
public bool birthInvoked;
//[HideInInspector]
public bool deathInvoked;
}
[System.Serializable]
public class ParticlesEvent : UnityEvent <ParticlesInfo>
{
[SerializeField] private GameObject Test1;
[SerializeField] public GameObject Test2;
[SerializeField]
private GameObject Test3;
[SerializeField]
public GameObject Test4;
}
As one can see, multiple tests have been made by me, trying it behind or above the line public and private and nothing has been shown in the Inspectyor at all.
Thank u for answering tho. It could also be, that I did something wrong rn. If I did pls tell me I am bad at this yes.
Answer by Maxim_Gamesnstuff_YT · Dec 28, 2021 at 10:05 PM
(Reposting this as an answer to me so eveyrbody sees it more clearly) @Casiell Well, I now have put SerializeFields all over my Code, to see if anything would show up in the Inspector:
[System.Serializable]
public class ParticlesInfo
{
[SerializeField] public ParticleSystem system;
public AudioClip sound;
public bool callEventsOnce;
[SerializeField]
public ParticlesEvent onBirth;
public ParticlesEvent onDeath;
//[HideInInspector]
public float[] m_times;
[SerializeField]
//[HideInInspector]
public ParticleSystem.Particle[] m_particles;
//[HideInInspector]
public bool birthInvoked;
//[HideInInspector]
public bool deathInvoked;
}
[System.Serializable]
public class ParticlesEvent : UnityEvent <ParticlesInfo>
{
[SerializeField] private GameObject Test1;
[SerializeField] public GameObject Test2;
[SerializeField]
private GameObject Test3;
[SerializeField]
public GameObject Test4;
}
As one can see, multiple tests have been made by me, trying it behind or above the line public and private and nothing has been shown in the Inspectyor at all.
Thank u for answering tho. It could also be, that I did something wrong rn. If I did pls tell me I am bad at this yes.
You're focusing on the wrong thing. ParticlesInfo and ParticlesEvent classes are ok (at least they seem ok). What you need to change is ParticleEvents class (pretty bad na$$anonymous$$g here, those classes have very similar names, easy to make mistakes)
Your ParticleEvents class is what shows up in the inspector. And there is no fields there, so nothing to serialize.
Move this line:
public ParticlesInfo[] Particles;
TO ParticleEvents class, it should be fine. The tutorial you are following pretty much skipped that step for whatever reason
MAAAAAAAAAAAAAAAAAAAAAAN It was taking me A WHOLE DAY for one line wooooooow. Dude thank u sooooooooooo much like really this was mental pain this was not good. But it's ok and the code shows up in the inspectore and all works and thank u and my gposh and aaaaaa ok im done now. And yeah it is my first time for working with classes and whatever the rest is. Its new for me so I really apreciate ur help.
@Casiell Also there i like another small bug in there, in the "finished code", but that I will move to another question. This here is solved and again thank uy very much.
Your answer
Follow this Question
Related Questions
Arrays/Lists in the inspector of Editor Extension scripts 0 Answers
How to move an object in boundaries of a cuboid or a cube? 0 Answers
Finding Particle Information Upon Collision 2 Answers
Add a new script line to my script with text from textboxes after button in the Inspector is pressed 0 Answers
There is no GameObject attached to this GameObject 2 Answers