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 WarPanda · Aug 11, 2013 at 11:09 PM · component

How do I replace a component with 'sent' component? Is this even possible in Unity?

So, I'm not sure if this is even possible, but what I'm trying to do is initialize a script/class on one object, adjust the relevant values in the script, then pass that script to a different object that handles the actual instantiation(sp?) of object using the altered script in place of it's default script of the same type. Hopefully that makes sense. But here's the relevant code:

 if(GUI.Button (new Rect ((Screen.width - TestBoxXPosition) + 25, (Screen.height - TestBoxYPosition) + 170, 
                         TestBoxWidth - 40, 25), "Build Pop Test"))
                     {
                         //Create temp PopScript attached to this object and adjust values appropriately
                         PopScript buildScript = gameObject.AddComponent<PopScript>();
                         buildScript.popType = PopScript.enPopType.Commerce;
                         
                         selectedPlanetScript.SendMessage ("BuildPop", buildScript);
                         
                         //Destroy temp PopScript
                         Destroy (buildScript);
                     }

That's the initial code, so when a button is pushed it creates a temp script attached to the GUI controller, updates the appropriate values (in this case the popType) then passes the script by sendMessage to the object actually building it. Here's that code:

 void BuildPop(PopScript buildScript) //receive the script for the pop
     {
         Vector3 buildPOS = transform.position;
         
         GameObject thisPop = Instantiate (popObject, buildPOS, transform.rotation) as GameObject;
         PopScript thisPopScript = thisPop.GetComponent<PopScript>();
         thisPop.transform.parent = gameObject.transform;
         
         //thisPopScript.popType = buildScript.popType;
         
         thisPop.SendMessage ("SetValuesInitial");    
         allPops.Add (thisPop);
         UpdatePopCount ();
     }

What I'm trying to do is essentially replace the default PopScript on 'thisPop' with the updated PopScript buildScript, without having to just go through and reassign all the values like I do in the commented out section in the middle.

Is this possible? Does it even make sense? Or is there a better way of doing this?

Also: How would I add buildScript as a Component of 'thisPop'? I tried

 thisPop.AddComponent<buildScript>();

but this doesn't work.

Thoughts? (Sorry if this is kinda hard to understand...I've been up for like 20 hours coding crap and I'm a bit tired)

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

Answer by LightStriker · Aug 12, 2013 at 02:15 AM

Why are you building a temporary Component in a GameObject that you delete just after? From what I understand, Unity doesn't let you pass around components. I never understood why GameObject simply doesn't have a IList of all Components so the user could play with them. But I guess it could create problem if a single component finds it's way on multiple GameObject at the same times. Again, Unity could be shielded against this kind of situation, but it's not.

Why not pass the parameters for this Component instead of a copy of the Component? Wrapping all the settings in a struct that can be passed around between components might make things easier. A kind of serialization without the problem of ripping an object apart and rebuilding it later. After all, what you want to pass around are only parameters.

 public class MyComponent : Component
 {
     public MyComponentSettings settings;

     public struct MyComponentSettings
     {
         public int param1;
         public float param2;
         ...
     }
 }

 public void SendMessage()
 {
     MyComponent.MyComponentSettings settings = new MyComponent.MyComponentSettings();
     // Change some params
     settings.param1 = 100;
     settings.param2 = 0.5f;
     selectedPlanetScript.SendMessage(settings);
 }

 public void BuildPop(MyComponent.MyComponentSettings settings)
 {
     GameObject go = new GameObject();
     MyComponent component = go.AddComponent<MyComponent>();
     component.settings = settings
 }

Does it make sense?

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

16 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

Related Questions

Accessing a function within a script assinged in inspector. 3 Answers

How can i remove two scripts from a GameObject that depend on eachother? 0 Answers

Reference Not Set toInstance of Object 1 Answer

DestroyByContact script component check mark disappearing 0 Answers

enable/disable specific components 3 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