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
1
Question by Houdini · Oct 29, 2012 at 05:40 PM · gameobjectinstance

How to add an existing script instance to a GameObject

My game will have hundreds of items/monsters and since I do not want to create a derived class for each of these items/monsters I use a single Monster and Item class, with fields for name, strength, damage, etc, that will differ for each type of item/monster. I have a factory class that will create the Monster/Item instance and automatically pass the field values via the constructor. In then end I can create a new item/monster by passing a monster type enum, like so:

 Monster goblin = Factory<Monster>.Create(Monster.Goblin);

The problem is when I create a new monster class instance I don't know how to attach this particular monster instance as a Component to the new GameObject. I really want to do something like this, which obviously does not work:

 Monster goblin = Factory<Monster>.Create(Monster.Goblin);
 GameObject game_obj = new GameObject(goblin.Name);
 game_obj.AddComponent(goblin);

Does anyone know how to add a particular script instance to a GameObject, or perhaps know of another way to do what I am trying to do?

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

2 Replies

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

Answer by whydoidoit · Oct 29, 2012 at 06:06 PM

Presumably your Monster class inherits from MonoBehaviour so it can be a component? Presuming it does then all you need to do is modify your factory method to take the game object you want to add the instance to.

 T Create(GameObject gameObject, SomeThing value)
  {
       var component = gameObject.AddComponent<T>();
       //Some other initialization
       return component;
 }

 

    
Comment
Add comment · Show 2 · 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 Houdini · Oct 29, 2012 at 07:46 PM 0
Share

This is a decent solution to the issue. Doesn't quite work in my case, as my factory object is actually a clone factory, so I have the item/monsters instances created beforehand like so: Factory.Register($$anonymous$$onster.Ogre, new $$anonymous$$onster("Ogre", 2, 13, 42, Location.All)); Factory.Register($$anonymous$$onster.Giant, new $$anonymous$$onster("Giant", 4, 26, 84, Location.All));

When the Factory.Create method is called it actually clones the existing instance, rather than simply creating one from scratch. However, turns out I can't create class instances using the 'new' keyword when they derive from $$anonymous$$onoBehaviour, so I need to change how I'm storing this information.

Still, I believe your solution and the one below solves my original issue.

avatar image whydoidoit · Oct 29, 2012 at 07:48 PM 0
Share

Yes you can only create $$anonymous$$onoBehaviours by using AddComponent<T>() which basically instantiates it and calls Awake() instantly.

avatar image
0

Answer by kmeboe · Oct 29, 2012 at 06:10 PM

Hey there,

If it were me, I would have the factory create the game object itself. That way, the factory can call AddComponent to create the script on the game object, and then populate the script's members before returning the completed GameObject. You may not be able to do this via constructor (since AddComponent will call the default constructor for you), but can use accessor methods instead.

Does this approach work for you?

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

11 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

Related Questions

How to add new instance of a script to a game object? 2 Answers

Is every Script I drag on a Gameobject an instance? 1 Answer

how can I display a variable as a GUIText 5 Answers

Create an object and assign code to it via script? 1 Answer

Help touch button 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