• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Superwayne · May 10, 2013 at 08:51 PM · custom class

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;
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chillersanim · May 10, 2013 at 08:58 PM 2
Share

Do you simply want to have a class that you can instantiate? Or do you want a global data manager? When you just want to instantiate, then simply create a new object from the class you've created, as long as you use the correct namespace, there should be no problem.

avatar image Superwayne · May 10, 2013 at 09:52 PM 0
Share

a global data manager. sorry didn't know the right expression for it..

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by chillersanim · May 11, 2013 at 07:17 AM

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. ;)

Greetings Chillersanim

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Superwayne · May 11, 2013 at 01:10 PM 0
Share

So I put my class [System.Serializable] public class Item{ public string name; public int costs; public float _value; public string itemClass; public int slotNumber; } in a seperate script in a namespace (for struction) and use an empty gameobject to store it?

So I can use "public Item[] items" in my shopSystem and also use it in the Inventory script?

I have to admit that I don't fully understand it but I guess that comes by trying around with it.

avatar image chillersanim · May 12, 2013 at 03:43 PM 0
Share

The first class I've shown is used to store the data. You can use the instances of this class where ever you wish. The second class I've used to store the instances somewhere global. You can write them as you want.

In case of a shop system, you will have a bit more complex structure. I'm not gona give you finished code, but I will help you to get it your self.

First think what your shop system should do. Then make a structure about what data you need for the shop system to work correcty. After that, think about how you want to store the data, how you want to access it, etc. Then implement the data classes. If you have done this, you can design your store and after that you can also implement it. It is important that you plan your code, whitout propper planing you will fail.

"Who fails to plan, plans to fail"

avatar image Superwayne · May 13, 2013 at 09:09 PM 0
Share

Hmm, at the moment I use an hashtable storing the properties of my class and pass it over and than create an new instance of my class and set all properties by using the objects in the hastable. It works but I guess it's not the best solution.

$$anonymous$$y plan is having only one guy for the shop because it's only a small game. All the information about the shop are stored on the $$anonymous$$ainCamera. The Information are divided in 3 classes:

 //This Class contains all information needed for the player charakter to buy and use it
     [System.Serializable]
     public class Item{
         [HideInInspector]
         public string name;
         public int costs;
         public float _value;
         [HideInInspector]
         public string itemClass;
         public int slotNumber;
         public Texture2D texture;
         public Texture2D guiTexture;
     }
     //This class contains the class above and the option if it's buyable or not in the shop and is used to create different items. I am not sure if I should let this one out and put everything directly in the Item class..
     [System.Serializable]
     public class BuyableItem{
         public string name;
         public Item _item;
         public bool buyable;
     }
     //This class is just to make the inspector more clearly
     [System.Serializable]
     public class itemClass{
         public string name;
         public BuyableItem[] buyableItem;
     }

So this I have to put in a plain c# script on an empty world object (no $$anonymous$$onobehaviour)?

But how can I make my classes now appear in the inspector?

avatar image chillersanim · May 14, 2013 at 03:58 PM 0
Share

To achiefe this, you need to add the script to a gameobject. Public fields are automaticaly shown. In your case, you should add the "itemClass.cs" script to a game object, because it is the container. You can also look at the other objects, because they are shown when you expand the detailview of a single item in the list. The "itemClass" class should possibly inherit from $$anonymous$$onobehaviour so it can work (I'm not sure about this).

avatar image
1

Answer by rutter · May 10, 2013 at 09:13 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by RafaelCN · May 10, 2013 at 10:37 PM

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
 {
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialization of custom class 1 Answer

More timer from same class 1 Answer

Inspector val init on non-mono class 1 Answer

Inspector - Show/hide property within custom class using enum 1 Answer

C# script can't access my JS class, even though its in Plugins 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges