• 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 smirlianos · Oct 29, 2019 at 10:57 PM · instantiatelistclassconstructor

How to add GameObject to List using a constructor method

I have this script attached to a GameObject prefab:

 //Room.cs
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Room : MonoBehaviour
 {
     public int sizeX;
     public int sizeZ;
 
     public Room(int minLength, int maxLength)
     {
         sizeX = Random.Range(minLength, maxLength);
         sizeZ = Random.Range(minLength, maxLength);
     }
 
 }


And this script on another GameObject that is supposed to create a list, and populate it with the first GameObject class. I want each gameobject to be created using the constructor method on Room.cs, but it doesn't work. I find it difficult to work with classes and gameobjects at the same time.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HouseCreator : MonoBehaviour
 {
 
     public int roomCount;
     public Room roomPrefab;
 
     public List<Room> rooms = new List<Room>();
     
     void Start()
     {
 
         for(int i = 0; i < roomCount; i++)
         {
             rooms.Add(roomPrefab(2, 6));  //I want to use the constructor here
             //Debug.Log("Room " + i + ": " + rooms[i].GetSizeX() + " X " + rooms[i].GetSizeZ());
             //rooms[i].CreateWalls(rooms[i].GetSizeX(), rooms[i].GetSizeZ(), i);
         }
     }
 
     
 }
 


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 Bunny83 · Oct 29, 2019 at 11:25 PM

You can not use constructors with MonoBehaviour derived classes. Those class instances are created by the runtime when you use AddComponent or when you instantiate a prefab. You generally can not and should not use constructors with MonoBehaviour derived classes. Instead you may want to use your own initialization method which you simply call after you created an instance of your class. You can design that Init method in a way to allow chaining. For example:

 public class Room : MonoBehaviour
 {
     public int sizeX;
     public int sizeZ;
     public Room Init(int minLength, int maxLength)
     {
         sizeX = Random.Range(minLength, maxLength);
         sizeZ = Random.Range(minLength, maxLength);
         return this;
     }
 }

By returning this we can directly use the return value somewhere else when we call it. To instantiate your prefab you would do

 rooms.Add(Instantiate(roomPrefab).Init(2, 6));

Note that the Awake method of your Room class (or any other component which might be part of the prefab) will run before the Instantiate call returns and before your Init method is called. You can not really bypass this as the actual creation of the class instance happens in the engine core. Awake will be called once the deserialization of the prefab has completed. Note that the Start callback will be called after your Init method since Start is always delayed just before the next Update call.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

List of Classes (js) - How to Construct Properly 1 Answer

change value of prefab field in a array of prefabs 0 Answers

A node in a childnode? 1 Answer

Saving a list from a class containing gamobjects 0 Answers

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges