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 Vice_Versa · Sep 23, 2015 at 03:01 AM · gameobjectprogrammingoop

Is there a way to create new GameObjects in your scene from constructors?

Im trying to teach object oriented programming to a class using unity, however, they find it pretty boring since were using debug.log statements to make "hypothetical gameobjects". Is there any way to instantiate new GameObjects in unity with constructors?

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

3 Replies

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

Answer by Vice_Versa · Sep 23, 2015 at 05:36 AM

i figured it out its quite possible, and more efficient in my opinion. Im going to always start coding like this.
using UnityEngine; using System.Collections;

 public class Enemy : MonoBehaviour {
 
     /**
      * Instance Variables
      * 
      * */
     private int health = 10;
     private Vector3 myLocation;
     private GameObject myObject;
 
     public Enemy()
     {
 
     }
 
     public Enemy(Vector3 location)
     {
         setLocation(location);
         setObject(Resources.Load("mySphere") as GameObject);
         health = 10;
         
     }
     // Use this for initialization
 
 
     public GameObject getObject()
     {
         return myObject;
     }
 
     public Vector3 getLocation()
     {
         return myLocation;
     }
     void setObject(GameObject inputObject)
     {
         myObject = inputObject;
     }
 
     void setLocation(Vector3 inputV)
     {
         myLocation = inputV;
     }
 }

//

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class main : MonoBehaviour {
 
     Enemy enemy1, enemy2, enemy3, enemy4, enemy5;
 
     Enemy[] enemies;
     ArrayList enemiesList;
     // Use this for initialization
     void Start () {
 
         enemy1 = new Enemy(new Vector3(1,1,1));
         enemy2 = new Enemy(new Vector3(2, 1, 1));
         enemy3 = new Enemy(new Vector3(3, 1, 1));
         enemy4 = new Enemy(new Vector3(4, 1, 1));
         enemy5 = new Enemy(new Vector3(5, 1, 1));
 
 
         enemies = new Enemy[5]{enemy1, enemy2, enemy3, enemy4, enemy5};
             for(int i = 0; i < enemies.Length; i++)
             {
                 Instantiate(enemies[i].getObject(), enemies[i].getLocation(), transform.rotation);
             }
 }
 }

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
0

Answer by Yword · Sep 23, 2015 at 03:42 AM

You can use the GameObject constructor to create a new GameObject.

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
0

Answer by Helical · Sep 23, 2015 at 05:08 AM

Unity is not very OOP friendly. Many of my workers which were OOP Thinkers had trouble thinking Unity wise.

You could teach that there are alternatives to OOP like Unity where instead of inhereting from a MonoBehavoiur class like Quest to add a field or a function. You could just make another component and attach it to the same GameObject and have it manage itself through the Start(). Thus having a different type of Quest Object even though no inheritance was used.

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

30 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

Related Questions

Multiple Cars not working 1 Answer

Control an gameobject in a specific area 0 Answers

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

how to show coordinates on the prefab? Using multidimensional arrays? 0 Answers

An OS design issue: File types associated with their appropriate programs 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