So...
I was watching some old unity tutorial videos and saw people editing properties of the elements of an array in editor like this:
When I tried in my project the editor does not showed the properties, just show a place to select an already created element:
Why the "Clip", "Volume" and "Name" properties are missing when I do it in my project?
My classes:
using UnityEngine;
public class AudioManager : MonoBehaviour
{
[SerializeField]
public Sound[] Sounds;
}
[System.Serializable]
public class Sound : MonoBehaviour
{
public AudioClip Clip;
public string Name;
[Range(0f, 1f)]
public float Volume;
[Range(.1f, 3f)]
public float Pitch;
}
I'm using Unity 2021.3.5f1
The problem is that your Sound class inherits from Object (MonoBehaviour), and Object are always serialized as reference. Make your Sound not inherit MonoBehaviour and it should look like in example (except that you have newer version of unity with reorderable list drawer)
Related
UI components and GameManager
I'm following a video tutorial on Unity and the person just dragged those items in the GameManager slots, what I am not able to do.
Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject ballPrefab;
public GameObject PlayerPrefab;
public Text scoreText;
public Text ballsText;
public Text levelText;
void Start()
{
}
void Update()
{
}
}
The problem is that you selected the GameManager inside your project folder and not inside your hierachy. Thats why you can drag & drop Prefabs inside of it but not your Text inside your hierachy.
I am trying to make a spell system. My base class for the system looks like this:
public abstract class MagicBook : ScriptableObject {
public Sprite bookSprite;
public float manaCost;
public float coolDown;
public bool rapidFire;
[HideInInspector] public bool coolDownElapsed;
public virtual void OnActivate() {
}
public virtual void OnActivate(Vector3 position, Quaternion rotation) {
}
}
Then I have another class extending from MagicBook where I override the OnActivate function. My problem now is that in the inspector only the variable bookSprite is showing and all the other values are not there. I tried adding an [SerializeField] in front of the variables and define them new in the extending class. But they still dont show. Has anyone an idea why they are not showing or how I can fix this?
Thanks for your help and time.
Did you create Asset for ScriptableObject?
Add attribute to extended class:
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/NewBook", order = 1)]
And then create asset with
Another idea:
Probably you have custom drawer for this object, but...
Turn your inspector into debug mode and check if properties are visible there.
Cannot reproduce this issue with Unity 2020.1.0f1.
public class NewBook : MagicBook
{
}
I have seen a similar error to this but for Video, however I can not find anywhere that explains my current error.
I will attach my code below for reference, does anyone know what the error means here?
using UnityEngine;
using System.Collections;
public class PlaySounds : MonoBehaviour {
public AudioClip SoundToPlay;
public float Volume;
AudioSource audio;
public bool alreadyPlayed = false;
void Start()
{
audio = GetComponent<AudioSource>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (!alreadyPlayed)
{
audio.PlayOneShot(SoundToPlay, Volume);
alreadyPlayed = true;
}
}
}
The MonoBehaviour class is already presenting a member called "audio" so your PlaySounds class has that member already. If you want to ignore the one from the base class you should declare it like so:
private new AudioSource audio;
But if you want the inherited member to be available as well as the one in your class simply give it a name other than "audio". To be honest unless you have a particular reason you want to override the inherited member I'd do the latter.
Using a basic script for an audio manager but I'm getting errors, not sure what's going on?
Tried "using System;" instead, same issues.
using UnityEngine;
[System.Serializable]
public class Sound {
public string name;
public AudioClip clip;
private AudioSource source;
public float volume = 0.7;
public float pitch = 1f;
public void SetSource (AudioSource _source) {
source = _source;
source.clip = clip;
}
public void Play () {
source.volume = volume;
source.pitch = pitch;
source.Play();
}
}
public class AudioManager : MonoBehaviour
{
[SerializedField]
Source[] sounds;
}
I expect this to add fields to the "Audio Manager" i'm building
Your error is related to this : https://docs.unity3d.com/ScriptReference/SerializeField.html
Which is an attribute supposed to be in UnityEngine namespace (assembly UnityEngine.CoreModule) and exist in several versions of Unity (so it doesn't appear to be a problem due to an API change across versions).
Also, did you write it properly ? I notice "SerializedField" instead of "SerializeField" in your error message, which seems weird. You should have a second look on it.
Are you sure to have all runtimes installed for Unity, all required "using" and assembly references, and all tools (Visual Studio) up to date ?
It's supposed to be [SerializeField], I had [SerializedField].
Sorry! Thanks for your reply AFract.
I can't declare or create the GameObject variable in C# unity i tried closing and opening the project restarted my PC but nothings working please help
Properties/Fields can only declared inside a class.
e.g.
public class MyClass
{
public GameObject dialogueManager;
}
if you want to use it as a Component for an Gameobject you have to derive from MonoBehaviour:
public class MyClass : MonoBehaviour
{
public GameObject dialogueManager;
}