• 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 30, 2015 at 05:32 PM · gameobjectclassextend

is there any possible way to extend from the GameObject class

im trying to use a more object oriented approach to a game im making, i noticed that if i do AddComponent and add a script to an object in my game, the update function on that object doesnt get called for some reason. Long story short i realized it would be much easier to accomplish what im going for if i could just derrive from the GameObject class. when i tried that i got an error that it cannot derrive from sealed type UnityEngine.GameObject. I know unity isnt too object oriented friendly, but is there any way to do this or do i have to refer to the gameObject component on my object to accomplish this?

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     /**
 
      * Class members
      **/
 
     int health;
     float speed; // meters per second
     GameObject obj;
     Vector3 location;
     string name;
 
 
     //the word function and method mean the same thing
 
     public Enemy(string name, GameObject myObj, Vector3 location)
     {
         this.name = name;
         obj = myObj;
         this.location = location;
         speed = 3;
         health = 10;
 
     }
     public Vector3 getLocation()
     {
         return location;
 
     }
 
     public GameObject getObject()
     {
         return obj;
     }
 
   
 
     public void setSpeed(float speed) //mutators & setters
     {
         speed ++;
     }
 
     public float getSpeed()
     {
         return speed;
     }
 
     public string getName()
     {
         return name;
     }
     
 }


 using UnityEngine;
 using System.Collections;
 
 public class Spawnner : MonoBehaviour { // spawner is extending from the Monobehaviour class (british people need to learn how to spell)
 
 
     Enemy Bob, Gene, Tina;
 
     // Use this for initialization
     void Start () {
 
         Bob = new Enemy("Bob", Resources.Load("mySphere") as GameObject, new Vector3(1, 0, 0));
         Gene = new Enemy("Gene", Resources.Load("mySphere") as GameObject, new Vector3(2, 0, 0));
         Tina = new Enemy("Tina", Resources.Load("mySphere") as GameObject, new Vector3(3, 0, 0));
 
         Instantiate(Bob.getObject(), Bob.getLocation(), transform.rotation);
         Instantiate(Gene.getObject(), Gene.getLocation(), transform.rotation);
         Instantiate(Tina.getObject(), Tina.getLocation(), transform.rotation);
 
         Bob.getObject().AddComponent<EnemyMovement>();
     }
     /**
     public void moveForward(Enemy myEnemy)
     {
         myEnemy.getObject().transform.position = new Vector3(myEnemy.getObject().transform.position.x, myEnemy.getObject().transform.position.y, myEnemy.getObject().transform.position.z + 1);
     }
     // Update is called once per frame
     void Update () {
       
         
     }
      * **/
 }



 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour{
 
     float speed;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         moveForward();
     }
 
     public void moveForward()
     {
         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
        
     }
 }
 


When i press play, i can see the script gets added to the prefab, but it doesnt move. If i press play again, it adds a second script to it and they all start moving(because they all have the same prefab). Im well aware there are much easier ways to move a gameobject in unity, but i really want to know why the script doesnt get used if i use AddComponent, and also from research i found out there is no way to extend the GameObject class.

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 tanoshimi · Sep 30, 2015 at 05:50 PM 0
Share

i noticed that if i do AddComponent and add a script to an object in my game, the update function on that object doesnt get called for some reason -> that's the problem you need to fix... please post your code.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by D3Duck · Sep 30, 2015 at 07:37 PM

To use AddComponent, it needs to be a MonoBehavior. Which it is so it should work. A GameObject can not be extended as far as I know. You are adding a script to the GameObject in the world and this is how Unity is designed to work.

So Tanoshimi is using the correct code:

 GameObject newBob = Instantiate(Bob.getObject(), Bob.getLocation(), transform.rotation) as GameObject;
 newBob.AddComponent<EnemyMovement>();

For example: when you right click in the hierarchy and select "Create Empty", you made a new GameObject.

Then when you click the new GameObject and then in the inspector click Add Component and then add your Enemy script, you have done what you would do with AddComponent().

Again, You are not supposed to "Add" to the GameObject. You are supposed to add a component to the list of components OF that GameObject with AddComponent();

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
2

Answer by tanoshimi · Sep 30, 2015 at 06:29 PM

You need to add the component to the instance you just created, (i.e. what's returned by Instantiate()) not to the gameobject you cloned (i.e. the parameter you supplied to Instantiate()).

Try this pattern around line 75:

 GameObject newBob = Instantiate(Bob.getObject(), Bob.getLocation(), transform.rotation) as GameObject;
 newBob.AddComponent<EnemyMovement>();
Comment
Add comment · Show 3 · 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 D3Duck · Sep 30, 2015 at 07:02 PM 0
Share

This is the correct solution. You are not adding your component to what you make but are adding it to something else, so of course Update() never gets called.

It doesn't get more object oriented than this =p

avatar image Vice_Versa · Sep 30, 2015 at 07:18 PM 0
Share

I see what you guys are saying, however i made a custom class called Enemy, which is not of type GameObject so AddComponent wont work. Hence my original question of can the GameObject class be extended

avatar image tanoshimi Vice_Versa · Sep 30, 2015 at 07:39 PM 0
Share

No it can't - it's sealed (but you already knew that because you stated it in your question). The correct solution is to add a component.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Accessing a GameObject parent Object 2 Answers

Instantiating gameObject with custom Class properties 1 Answer

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

Finding class where current GameObject is assigned to as a var 1 Answer

Is every Script I drag on a Gameobject an instance? 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