How can I access internal class from another class ?

I have this script with some variables :

using System;
using UnityEngine;
using Random = UnityEngine.Random;

namespace UnityEditor.TreeViewExamples
{
   [Serializable]
   internal class MyTreeElement : TreeElement
   {
       public float floatValue1, floatValue2, floatValue3;
       public Material material;
       public string text = "";
       public bool enabled;

       public MyTreeElement (string name, int depth, int id) : base (name, depth, id)
       {
           floatValue1 = Random.value;
           floatValue2 = Random.value;
           floatValue3 = Random.value;
           enabled = false;
       }
   }
}

And I want to save/load this variables values from this script :

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class SaveData
{
   public static void Save()
   {

   }

   public static void Load()
   {

   }
}

But when trying to type MyTreeElement in the Save() like this :

public static void Save(MyTreeElement)

The MyTreeElement is not exist.

First of all, there needs to be an identifier for the parameter when you declare a method signature.

public static void Save(MyTreeElement treeElement) { ... }

Now, if those types are within the same assembly, then there’s nothing special that you have to do.

Assuming that’s the case, then you’re likely missing the using directive at the top of the file that wants to use your type MyTreeElement.

You can either specify that you want to have access to all types in namespace UnityEditor.TreeViewExamples, as follows:

using UnityEditor.TreeViewExamples;

// later in that file
public static void Save(MyTreeElement treeElement) { ... }

or you can use the full qualified name (FQN) of the type:

public static void Save(UnityEditor.TreeViewExamples.MyTreeElement treeElement) { ... }

Small note: Avoid putting your own types into namespaces that already exists, e.g. UnityEngine, UnityEditor, System, and so on.

1 Like

The problem is that when I’m doing in the SaveData class at the top :

using UnityEditor.

There is no TreeViewExamples the TreeViewExamples is not exist.
Same if I’m doing inside the Save method :

public SaveData(UnityEditor.TreeViewExamples.MyTreeElement)

The MyTreeElement not exist.

But I saw in another script no my one that the MyTreeElement does exist and can be accessed :

using System.Collections.Generic;
using UnityEngine;

namespace UnityEditor.TreeViewExamples
{
   
    [CreateAssetMenu (fileName = "TreeDataAsset", menuName = "Tree Asset", order = 1)]
    public class MyTreeAsset : ScriptableObject
    {
        [SerializeField] List<MyTreeElement> m_TreeElements = new List<MyTreeElement> ();

I can’t figure out why it’s exist can be access here but on in my class.
I tried to change the SaveData class to add to it the namespace namespace UnityEditor.TreeViewExamples but it didn’t help. I tried to make the internal class to be public without internal but not working.

Tried to make the SaveData class to be type ScriptableObject it should not be ScriptableObject but for testing but it’s not working either.

Nothing I can’t access the MyTreeElement from my SaveData class can’t figure out why bot and in other scripts it can be accessed.

Ok I found how to make it work.

I just moved the script SaveData into the folder of the of the other Tree…scripts in the Assets in the editor.
Now I have access to all the variables in MyTreeElement
But I still can’t figure out why after moving the script to the folder make it working.

Where was this script located before you moved it?
All scripts have to be in the asset folders so that Unity takes them into account. You cannot just have them anywhere else on your development device.

Sometimes they have to be in a special subfolder, but that’s another topic.

1 Like

It was at the main Assets then I moved it to the other treeview scripts folder and it’s working.