- Home /
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)
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?
Your answer

Follow this Question
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