Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

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

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

How can I instantiate a object with a constructor? 3 Answers

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

A node in a childnode? 1 Answer

How do I Instantiate a prefab in a randomly generated location specific to tile type (2D Procedural game) 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