// paste code here
Pls see pic above
sry I really dont know how to explain this qustion…
is there any way to make:
public hairComponments xxx = new hairComponments();
is legal in hairProperties
, but not legal in any other class
im a beginner of C#, and i currently confuse on it, thank you for answering the ques.
Hi @Kishin_Sag,
If I’m understanding your question correctly, it sounds like you want to set up the “constructor” of the “hairComponents” class to only be callable from within the “hairProperties” class. The “constructor” is the “hairComponents()” part of your code, which constructs a new hairComponents object.
This is all defined by the implementation of C#, and is not specific to Unity, and unfortunately there is probably not a satisfying answer to this. You have essentially have 2 options:
- You can make “hairComponents” a private child class of “hairProperties”, but doing so will mean that you’ll also need to make “backHair” and “foreHair” private to match the “scope” - and I’m guessing you still want to access those members from other classes (or serialize them so you can see them in Unity’s Inspector). You can also make make “hairComponents” “Serializable” by adding the [Serializable] tag above the line “private class hairComponents”, and adding the [SerializeField] tag before the backHair and foreHair member declarations. All of that combined will get you into a state where only hairProperties can see hairComponents, since it is a child class, and thus is the only place you can construct hairComponents, and it will still be visible in the inspector, because it is being serialized.
- You can mark the hairComponents class as “internal”, which will make it so that it can’t be utilized from other “assemblies”, but it’ll still be visible to other classes within the same “assembly”. I’m guessing all your code is in the default assembly, though, so this won’t really do much for you.
Here’s an example of #1:
using System;
using UnityEngine;
public class MainSampleClass : MonoBehaviour
{
[SerializeField] private ChildSampleClass sampleThingy = new ChildSampleClass();
[Serializable]
private class ChildSampleClass
{
public float someNumber = 3.14f;
}
}
Which will look like this in the inspector: