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
0
Question by pumpkinszwan · Jul 01, 2014 at 02:28 PM · c#addcomponent

Copy a script from one gameobject to another?

At runtime I want to be able to switch an item between completely different behaviours, which I think is best done by swapping out a script (inherited from a master 'ItemBase' class, e.g. Item_Wood, Item_Stone, Item_Car). The items have custom methods, so they require their own scripts.

So to switch a gameobject from 'stone' to 'wood'...

I can add a script:

 InputItem.AddComponent<Item_Stone>();

(after using Destroy to remove the existing 'Item_Wood' script).

But how do I do this when I don't know beforehand what the scripts specific types are?

I want to be able to dynamically copy an ItemBase script (of unknown type) from one gameobject to another, something like this (the following code doesn't work):

 ItemBase ibase = ObjectA.GetComponent<ItemBase>(); // ibase could be any class inherited from ItemBase
 ObjectB.AddComponent<ibase.GetType>(); // want to add a class of the same type as ibase


The script will always be inherited from ItemBase, but I would not know beforehand what the actual script type is (it could be one of dozens of scripts, so hardcoding a switch statement is not something I want to do.

Or am I going about this completely the wrong way? The main result I'm after is to morph objects by interacting with each other, but in a very dynamic way (e.g. an item could change in hundreds of different ways based on different interactions). I want to keep my game objects intact, but change their behaviours completely by switching scripts.

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
1
Best Answer

Answer by Aeron0 · Jul 01, 2014 at 03:22 PM

Instead of having scripts, couldn't you just make a base class and inherit the other classes from that? Then make the inherited class methods virtual so the inherited classes could have their own custom methods.

By setting it up that way you could throw any of those classes in a base class reference variable. Maybe something like:

 public class Item
 {
   public GameObject myGameObject;
 
   // Additional Variables 
 
 
   public virtual void Method1()
   {
 
   }
 
   public virtual void Method2()
   {
 
   }
 
 
 }
 
 
 public class itemWood:Item
 {
     // Variables
 
   public override void Method1()
   {
 
   }
 
   public override void Method2()
   {
 
   }
 
 }
 
 public class itemStone:Item
 {
     // Variables
 
   public override void Method1()
   {
 
   }
 
   public override void Method2()
   {
 
   }
 
 }


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 pumpkinszwan · Jul 01, 2014 at 03:28 PM 0
Share

Thank you. This is sort of how I initially tackled it, then got lost when I wanted public fields for the inspector. But I can keep the public fields in a different script as they are generic to all objects. I think I overthought the problem! This solution should work.

thanks

avatar image Aeron0 pumpkinszwan · Jul 01, 2014 at 04:00 PM 0
Share

You could also create an override method for each inherited class that would create the same class as it. In that way if you randomly created one of the inherited classes and referenced it and wanted another reference to have that same type of class but a completely different object itself you could and wouldn't need to know anything before hand or need a big switch statement.

// itemA is randomly itemStone but we wouldn't know that at runtime

// itemB wants to be the same thing as itemA but we dont know what itemA is

public itemB;

//If we have an override method that creates the same object then we don't need to know or have a switch statement.

itemB = itemA.CreateItem();

itemB is now the same type of object as itemA

Example

 public class itemStone:Item
 {
   // Variables
  
   public override void $$anonymous$$ethod1()
   {
  
   }
  
   public override void $$anonymous$$ethod2()
   {
  
   }
 
   public override Item CreateItem()
   {
     Item item = new itemStone();
     return item;
   }
 
 }

  

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Joints Configured from Script do not work properly? $20 reward 3 Answers

Disabling a script in c# not working for some reason 1 Answer

Same GUI script showing up in different places 0 Answers


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