Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 gdubrocks · Jul 01, 2014 at 12:14 AM · c#monobehaviour

Not properly understanding how monobehaviour scripts act without a GameObject.

I have three files(scripts), which each contain a class of the script name. Account, Player(MonoBehaviour) and GameManager (MonoBehaviour).

Account is essentially a save slot. It holds a users gold, status in the game, and most importantly a list of players ( public Player[] team ).

Player holds the attributes that a player(character) should have (hp, attack power...). These variables need to exist even when the game is not actively being played.

GameManager is what creates and allows the game to be played. It contains a list of gameObjects that will physically represent the player.

I was told previously to make my player and Account scripts not inherit from MonoBehaviour, but this means my gameObjects won't have the player script attached to them, which makes interactions funky.

So I decided to make Player a monobehaviour script, but if I do this I have trouble keeping scripts without a GameObject.

My questions are : Can I have a script without a GameObject? If not, should I make team(within account) a GameObject array that stores the players? If not, what is the best way to implement this?

 public class Account{
 
     private string accountName;
     public int gold;
     public int teamSize = 15;
     public int playerCount = 0;
     public Player[] team;
     //public GameObject[] team;
     
     public Account(string newAccountName){
         accountName = newAccountName;
         gold = 10;
 
         team[0].newPlayer("Garet", "Mage", 0);
         team[1].newPlayer("Karla", "Ranger", 0);
         team[2].newPlayer("Tyler", "Warrior", 0);
         
         playerCount = 3;
     }
 }
 
 public class Player : MonoBehaviour{
 
     public string playerName;
     public string className;
     public int experience;
 
         public int health... etc....
 
 //constructs a new player
     public void newPlayer(string newCharName, string newCharClass, int newExperience)
     {
         playerName = newCharName;
         className = newCharClass;
         experience = newExperience;
         
         setStats();
     }
 }
 
 public class GameManager : MonoBehaviour {
 
     //other scripts
     public Player player;
     public Account currentAccount;
 
     //Stores the gameObjects
     List<GameObject> playerModels = new List<GameObject>();
     
     //Awake is the first thing called, and instantiates all scripts
     void Awake()
     {        
             currentAccount = new Account("tempAccount");
     }
     
     //Start is called just after Awake, and instatiates all players and global variables
     void Start()
     {
         //create representations of players
         addPlayers(2);
     }
     
     //adds numberOfPlayers many plays from account into game.
     void addPlayers(int numberOfPlayers)
     {
         //loop through numberOfPlayers and create gameObjects for them.
         for(int i=0; i < numberOfPlayers; i++)
         {
             //creates a capsule gameObject to represent the player
             //playerModels[i] = GameObject.CreatePrimitive(PrimitiveType.Capsule);
             playerModels.Add(GameObject.CreatePrimitive(PrimitiveType.Capsule));
             
             //moves the capsule to the start position determined by its number
             playerModels[i].transform.position = spawnPosition(i);
             
             //tags the gameObject as a player
             playerModels[i].tag = "Player";
             
             //playerModels[i].AddComponent<Player>();
             playerModels[i].AddComponent<Player>();
             Player tempPlayerScript = playerModels[i].GetComponent<Player>();
             tempPlayerScript.copyPlayer(currentAccount.team[i]);
             
             //TODO set player team??
             
         }
     }
 
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dracorat · Jul 01, 2014 at 12:27 AM

I think you hit the nail on the head in stating you don't understand why they should or should not be inherited from MonoBehavior.

MonoBehavior is itself a class that allows your objects to be attached to objects that Unity is managing so that it can manage them as a special object type of its own.

In short, if you're attaching a class to an object, it should always inherit from MonoBehavior.

However! If you have created a class that is not directly tied to in-game objects (examples might include a class to manage scores, or game-wide settings, or logical classes of data that have no in-game object such as save / load code) then you are adding the overhead of a class by inheriting from MonoBehavior with no added gain.

To put it another way, if your classes directly call the class in question, but do not attempt to attach that class to a Unity Object (actor, etc) then don't inherit from MonoBehavior. If your object violates these, inherit.

If in doubt, you can inherit - you most likely just cost yourself some RAM by doing so.

Comment
Add comment · Show 2 · 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 gdubrocks · Jul 01, 2014 at 01:13 AM 0
Share

So my classes do have the proper type then, Account is not monobehaviour, but the other two should be.

Now how should I handle the "$$anonymous$$m" variable within Account?

Should I change it to a bunch of game object that hold a player script? Should I leave it the way it is? If so, I am getting a null reference whenever I try to access the $$anonymous$$m, how do I initialize the scripts?

avatar image Dracorat · Jul 01, 2014 at 04:29 PM 0
Share

Team variable: looks fine. Though I'd use a List ins$$anonymous$$d and initialize it in the constructor - but that's a personal style choice and not any more or less "correct".

As for accessing other scripts, there's information I don't have. From what I can tell, it looks like you've attached them to objects in which case you just need to wire them up in the Unity designer.

If you're doing it without $$anonymous$$onoBehavior, you usually just create the class, or if you want to use a singleton, create a singleton pattern or make it static.

Singleton pattern: http://msdn.microsoft.com/en-us/library/ff650316.aspx

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.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Awake not called if class has static member 2 Answers

Coroutine without MonoBehaviour 6 Answers


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