how to make a class storing properties available from all scripts

Hey,

I got a serialized class to store the informations for my ingameitems so it becomes more clearly and easy to set in the inspector. Now I need the same class in another script. Is there a other way than declaring the class again?
Static is not option because I need more instances of that class.

This is the class:

[System.Serializable]
public class Item{
	public string name;
	public int costs;
	public float _value;
	public string itemClass;
	public int slotNumber;
}

To make a global data management, you could create a empty object, which stores and manages the data. For that you need the class that holds the data and you need a script that manages the data inside this empty.

The data class can look somehow like this:

using System;

namespace GlobalDatas
    {
	public class GlobalScore
	{
		public string Playername;
		
		public int Score;
		
		public GlobalScore ()
		{
			Playername = string.Empty;
			Score = 0;
		}		
	}
}

And the managing script can look somewhat like this:

using System;
using GlobalDatas;
using System.Collections.ObjectModel;

class ScoreManagement : MonoBehaviour
{
    // Creates a public readonly list with only get access 
	public ReadOnlyCollection Scores 
	{
		get
		{
			return scores.AsReadOnly();
		}
	}

    // The manager self works only with the private field
	private List scores;
	
	void Start()
	{
		scores = new List();
	}

    // Only methods like this one can be used to modify the content of score
	public void AddScore (GlobalScore score)
	{
		scores.Add(score);
	}
}

The first class only contains the data. I put it into a new namespace to make the project more structured, but you don’t need to do that.
The second class manages the scores. The public property has only a get access to prevent external editing of the data, you can also make the whole thing fully accessible by just making the private score public.

I hope that I could help a bit, when the code is to complex, just say a word and I will simplify it a bit. :wink:

Greetings
Chillersanim

You could create a static list of objects, adding and removing from the list as the objects are created.

Something like this:

using UnityEngine;
using System.Collections.Generic;

public class Global : MonoBehaviour 
{
	static List<Global> globals = new List<Global>();
	
	void Start()
	{
		globals.Add(this);
		if (globals.Count == 1) Clone();
		else Invoke("Suicide", 2f);
	}
	
	void Clone()
	{
		Instantiate(gameObject);
		Invoke("Clone", 0.5f);
		Debug.Log(globals.Count + " globals so far");
	}
	
	void Suicide()
	{
		Destroy(gameObject);
	}
	
	void OnDestroy()
	{
		globals.Remove(this);
	}
}

That example isn’t particularly useful, but does serve to demonstrate the basic concept. You’ll probably want to create a slightly different structure, depending on what exactly you’re trying to do.

In particular: what are these objects for? What components need to access them, and in what ways? Do you need some way to identify which object is which? Are they associated with anything in particular?

Do you care if the objects are deleted between scenes? Do they need to be a MonoBehaviour? If they do, they’re a bit harder to manage, but ultimately it’s not a big deal either way. Just be careful to avoid corrupting your list.

you can make this:

public abstract class Dad : MonoBehaviour 
{
    
   }

and then in other script you do that to get the inheritance of the dad’s class.

 public class son : Dad
{

}