• 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 neweichgames · May 25, 2015 at 01:25 PM · scriptingproblemaddcomponent

Adding a script from another game object

I am trying to add my script from a game object to another game object by trying to add component but I want it to have the same custom variables. I am wondering if I could add a component to the game object and change the variables to match the other game object. Here is my script so far...

 GameObject cpuClone = Instantiate (Cpu, new Vector3 (6, -2, 0), Quaternion.identity) as GameObject;
             cpuClone.GetComponent<SpriteRenderer> ().sprite = player2;
             cpuClone.name = Cpu.name;
 
         
             PlayerCPU cpu = cpuClone.AddComponent<PlayerCPU>();
             cpu = transform.FindChild("Abilities").GetComponent<PlayerCPU>(); //Trying to have the same script as the gameobject Abilites
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
Best Answer

Answer by barbe63 · May 25, 2015 at 01:48 PM

You need to set the value you need one by one.

I see 2 ways of doing it, the second is IMO better. The first:

     cpuClone.AddComponent<PlayerCPU>();
     cpuScript = cpuClone.GetComponent<PlayerCPU>(); //cache it like this so you won't need multiple GetComponent
     cpuScript.value1 = Cpu.value1; 
     cpuScript.value1 = Cpu.value2; 
 //etc...
 //your values have to be public so it's not ideal
 

The second is having a function in your class like this:

 public void GetValues(int _value1, string _value2) //as many parameters as values you need
 {
     value1 = _value1;
     value2 = _value2;
     //etc...
 }

Then call it after instantiating like this:

 cpuClone.AddComponent<PlayerCPU>();
 cpuClone.GetComponent<PlayerCPU>().GetValues(value1, value2) //etc...
 
 

I wonder why you need to add the component on run, wouldn't it be better to have it in the prefab?

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 starikcetin · May 25, 2015 at 02:30 PM 0
Share

$$anonymous$$aybe he is making some procedurally generated characters.

avatar image neweichgames · May 25, 2015 at 05:21 PM 0
Share

Ok thank you I don't have it as a prefab because I am trying to make a level select where you select the player and change the player abilities values on one player prefab. I don't want to make a prefab for every player in the game and set each abilities.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to child multiple GameObjects to one master object in script 1 Answer

How Do I Get a Script to Reference the GameObject When Using AddComponent? 1 Answer

Creating prefab through custom editor window 1 Answer

AddComponent adds too many (C#) 3 Answers

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent() 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