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 MCHammond · Nov 10, 2013 at 01:32 AM · gameobjectlistsmonobehaviourconstructors

How does one go about creating a list of GameObjects?

How does one go about creating a list of GameObjects?

I have created a class called Hex it has the follow variables:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Hex : MonoBehaviour {
         
     [HideInInspector] public GameObject hex;
     [HideInInspector] public int[] weightValue;
     [HideInInspector] public GameObject[] adjacentHexs;
     [HideInInspector] public GameObject[] connected;
     
     static public List<Hex> hexList = new List<Hex>();
     
     
     //Defualt constructor
     public Hex(GameObject addHex)
     {
         hex = addHex;
     }
     
     //Advanced constructor
     public Hex(GameObject addHex, int[] addWeightValue, GameObject[] addAdjacentHexs, GameObject[] addConnected)
     {
         hex = addHex;
         weightValue = addWeightValue;
         adjacentHexs = addAdjacentHexs;
         connected = addConnected;
     }
     
     // Use this for initialization
     void Start () {
         
         foreach (GameObject hexEmpty in GameObject.FindGameObjectsWithTag("Hex"))
         {
             print("Adding to list");
             hexList.Add (new Hex(hexEmpty));
             
         }
     }
 }

I also have this script:

 using UnityEngine;
 using System.Collections;
 
 public class HexMaster : MonoBehaviour {
     
     
     void Start () 
     {
         foreach (Hex hexItem in Hex.hexList)
         {
             print(hexItem.hex.transform.position.ToString());
         }
             
     }
 }

But when I run the script I get the following error:

"You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all"

How would one go about creating a list of gameobjects without using MonoBehaviour?

Comment
Add comment · Show 1
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 MCHammond · Nov 10, 2013 at 01:35 AM 0
Share

The code works I just get the error/warning so I am guessing there is a "proper" way of doing this that I cant figure out?

1 Reply

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

Answer by VioKyma · Nov 10, 2013 at 02:24 AM

You cannot use a constructor for a MonoBehaviour or use the "new" keyword (same goes for GameObjects). You want to use Instantiate and add the returned game object to the list. Unity already has a constructor that runs on these things, so writing your own breaks Unity's functionality. You have one of two options here: 1. Remove the MonoBehavior, and lose the inspector functionality OR 2. Stop using the constructors, and instead use Start() and other methods to initialise your objects.

To create the gameObject list do something like the following:

 static public List hexList;
 
 void Start()
 {
     hexList = new List<Hex>();
 }

To add items to the list do something like this:

 GameObject hexItem = Instantiate(...instantiation details...);
 hexList.Add(hexItem);

If you want a list of prefabs to be available to then instantiate later, you need to use the Inspector to assign these.

Let me know if you need more detail or have any questions.

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 VioKyma · Nov 10, 2013 at 02:34 AM 0
Share

To clarify the final statement a bit, Prefabs are only available to your script if they are passed in via the Inspector.

avatar image MCHammond · Nov 10, 2013 at 02:35 AM 0
Share

Thank you for your help. I am not sure how I would use Instantiate the GameObjects I want to add are already in the scene?

avatar image MCHammond · Nov 10, 2013 at 02:44 AM 0
Share

Im not sure I understand,

I have a list that holds a class called "Hex" the hex class has a variable that is a gameobject.

So I need to add a new Hex before I can add its gameobject?

avatar image MCHammond · Nov 10, 2013 at 03:15 AM 0
Share

O$$anonymous$$ I think I have this sorted now thanks Vio$$anonymous$$

I wrongly assumed that when you created a script without inheriting from $$anonymous$$onoBehaviour that you lost ALL access to Unity Stuff but you can still use a GameObject as variables.

Code looks like this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Hex {
     
     public GameObject hex;
     public int[] weightValue;
     public GameObject[] adjacentHexs;
     public GameObject[] connected;
     
     static public List<Hex> hexList = new List<Hex>();
     
     //Defualt constructor
     public Hex(GameObject addHex)
     {
         hex = addHex;
     }
     
     
 }

and

 using UnityEngine;
 using System.Collections;
 
 public class Hex$$anonymous$$aster : $$anonymous$$onoBehaviour {
     
     
     void Start () 
     {
         //Add Hexs to list
         foreach (GameObject hexEmpty in GameObject.FindGameObjectsWithTag("Hex"))
         {
             Hex.hexList.Add (new Hex(hexEmpty));
         }
         
         //print there locations
         foreach (Hex hexItem in Hex.hexList)
         {
             print(hexItem.hex.transform.position.ToString());
         }        
     }
 }

Thank you again :D

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

17 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

Related Questions

A node in a childnode? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Trash Collection For Instantiated Game Object 1 Answer

C# Rotate GameObjects Regardless of List Size 2 Answers

Assigning a GameObject to a class 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