Hi I’m a noob in C# and Unity and I try to translate some basics script from javascript to C#.
Got a lil problem with public variable created in a class to recall them outside the class in order to have them in the inspector.
thats the picture
public class ClassA:Monobehaviour
{
// this variable appear in the inspector no problem
public float myWonderfullVar = 0.0f;
// My new Class with some settings inside
ClassB
{
// those variable are stuck in here i want to recall them in the inspector
public float jumpHeight = 2.0f;
public Vector3 someVarWhatEver = Vector3.zero;
}
// The function must be recall there but how???
public ClassB newSetUp = new ClassB(); //??? this is not working
}
Should I create a getter or something like that. Please help me many thanks.
If you want an instance variable of another class to be visible in the inspector, the class that contains that variable needs to be serializable. That’s why there is the [System.Serializable] thing in front of the ClassB declaration. You don’t have to create an instance of ClassB in that situation.
As far as I know that is the only way to achieve the visibility in the inspector.
You should try to be a little bit more careful about cases and keywords.
using UnityEngine;
using System.Collections;
public class ClassA : MonoBehaviour
{
// this variable appear in the inspector no problem
public float myWonderfullVar = 0.0f;
// The function must be recall there but how???
public ClassB newSetUp;
}
[System.Serializable]
public class ClassB {
// those variable are stuck in here i want to recall them in the inspector
public float jumpHeight = 2.0f;
public Vector3 someVarWhatEver = Vector3.zero;
}
Arf this is not working. But i think i didn’t explain to clearly.
I try to translate the PlatformerController.js script from the 2D unity tutorial. inside they are creating some class with regrouped variables (some are serialized other are not). the intial script look like this (juste an extract)
class PlatformerControllerMovement {
// The speed when walking
var walkSpeed = 3.0;
// when pressing "Fire1" button (control) we start running
var runSpeed = 10.0;
var inAirControlAcceleration = 1.0;
// The gravity for the character
var gravity = 60.0;
var maxFallSpeed = 20.0;
// How fast does the character change speeds? Higher is faster.
var speedSmoothing = 5.0;
// This controls how fast the graphics of the character "turn around" when the player turns around using the controls.
var rotationSmoothing = 10.0;
// The current move direction in x-y. This will always been (1,0,0) or (-1,0,0)
// The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view. Very handy for organization!
@System.NonSerialized
var direction = Vector3.zero;
// The current vertical speed
@System.NonSerialized
var verticalSpeed = 0.0;
// The current movement speed. This gets smoothed by speedSmoothing.
@System.NonSerialized
var speed = 0.0;
// Is the user pressing the left or right movement keys?
@System.NonSerialized
var isMoving = false;
// The last collision flags returned from controller.Move
@System.NonSerialized
var collisionFlags : CollisionFlags;
// We will keep track of an approximation of the character's current velocity, so that we return it from GetVelocity () for our camera to use for prediction.
@System.NonSerialized
var velocity : Vector3;
// This keeps track of our current velocity while we're not grounded?
@System.NonSerialized
var inAirVelocity = Vector3.zero;
// This will keep track of how long we have we been in the air (not grounded)
@System.NonSerialized
var hangTime = 0.0;
}
var movement : PlatformerControllerMovement;
Its more looking like a struct for me than a class but using struct is impossible when you use NonSerialized. and at the end they call the class by a variable “movement”. This variable is the one who appear in the inspector at the end. I wonder how to translate this piece of code.
that is my nonworking try:
public class PlatformerControllerMovement
{
// Vitesse de marche
public float walkSpeed = 3.0f;
// Vitesse de course
public float runSpeed = 10.0f;
// Controle de l'accélération dans l'air
public float inAirControlAcceleration = 1.0f;
// Gravité du personnage
public float gravity = 60.0f;
public float maxFallSpeed = 20.0f;
// A quel vitesse le perso change de vitesse
public float speedSmoothing = 5.0f;
// Vitesse de rotation des graphismes quand le perso fait demi-tour
public float rotationSmoothing = 10.0f;
// Variables NonSerialized ------------------------------------------
// Direction des mouvement actuelle
[System.NonSerialized]
public Vector3 direction = Vector3.zero;
// Vitesse verticale actuelle
[System.NonSerialized]
public float verticalSpeed = 0.0f;
// Vitesse de déplacement actuelle
[System.NonSerialized]
public float speed = 0.0f;
// Le joueur presse gauche ou droite
[System.NonSerialized]
public bool isMoving = false;
// Retour du dernier point de collision
[System.NonSerialized]
public CollisionFlags collisionFlags = CollisionFlags.None;
// Vélocité actuelle du personnage
[System.NonSerialized]
public Vector3 velocity = Vector3.zero;
// Vélocité en l'air
[System.NonSerialized]
public Vector3 inAirVelocity = Vector3.zero;
// Garde une trace du temps rester en l'air
[System.NonSerialized]
public float hangTime = 0.0f;
}
// Variable pour afficher les variable paramétrables dans l'inspector
public PlatformerControllerMovement movement;
Declare ClassB outside ClassA and use [System.Serializable] on the line directly above the ClassB definition. Then, instead of creating a new instance of ClassB with the constructor, just declare a public variable of type ClassB in ClassA:-
[System.Serializable]
public class ClassB {
public float x;
...
}
public class ClassA : MonoBehaviour {
...
public ClassB bInstance;
...
}
In this case, you would be able to see “bInstance” in the inspector and set its variables.