How do I swap gameObjects in a single-player game using C#?
I'm making a 2.5D platformer and my character has two different forms of movement: one is an avatar with regular walking/jumping and the other is a rolling sphere. They're meant to act as a single character and I would like to be able to swap from one GameObject to another whenever [space] is pressed down, maintaining the same location as before. I believe it's just a matter of switching the mesh rendering on and off between the two objects, however, they would move at different speeds. I'm still pretty new to Unity and C# so I'm not sure how to make this happen in the script.
I appreciate any help.
Here is a simple script showing you how to change your mesh. sphere$$anonymous$$esh
is some mesh you choose in the editor.
$$anonymous$$eshFilter meshFilter;
public $$anonymous$$esh sphere$$anonymous$$esh;
void Start ()
{
meshFilter = GetComponent<$$anonymous$$eshFilter> ();
meshFilter.mesh = sphere$$anonymous$$esh;
}
You'd also enable and disable the colliders that fits each mesh as well.
I need more info from you tho to help you further. How differently do you want the rolling sphere to act? Is it just speed or is there more?
Both objects are spherical in shape, but the "sphere" mode will be rolling using Unity's physics.
$$anonymous$$y main question is: how can I switch between character 1 and character 2? Essentially, when I press spacebar, character 1 will disappear and character 2 will take its place and vice verse.
I'll keep the mesh and colliders in $$anonymous$$d, but the switching is the important part. :)
Answer by LiamofElites · Feb 14, 2018 at 12:47 AM
I think this would work but it's complex. Also if the gameobject has children or components you'd like to keep each time it's created this could be very useful
-create an empty gameobject rename it to whatever (I'm going to call it playerContainer)
--(if you already have not created the Resources folder for Resources.Load go ahead and do that in the assets folder
-drag the player form gameobjects you've already made into the Resources folder
-Implement the following into the script on playerContainer
int form = 1;
float speed = 1f;
void Start(){
Instantiate(Resources.Load("playerForm1"));
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
if(form == 1){
form++;
Destroy(gameObject.transform.GetChild(0));
Instantiate(Resources.Load("playerForm2") as GameObject);
//since you mentioned speed reduction I'm guessing you want the speed to be changed. Change the code if this isn't the speed you want
speed = 0.5f;
}else{
form--;
Destroy(gameObject.transform.GetChild(0));
//Resouces.Load basically looks for the prefab in the Resources folder. Just for reference
I called the form gameobject playerForm1 and playerForm2. You can keep what you already
have just change the names
Instantiate(Resources.Load("playerForm1") as GameObject);
speed = 1f;
}
}
}
-Make sure that any movement scripts are on the playerContainer GameObject.
I really hope this can help you somehow, if you have any questions or comments please contact me!
@LiamofElites Thanks for the reply! Unfortunately, I had a few errors: it asked for the suffix f to be added to the initial float speed = 1
and I changed it to float speed = 1f
which satisfied it. Next, the method group of GetChild[0]
wouldn't accept brackets []
so I changed them to <>
and the error went away. The last persisting error is that at the end of the altered gameObject.transform.GetChild<0>)
; line, there is an invalid expression term for both closing parentheses. Tried different iterations but it didn't work, any ideas?
Assets/Scripts/PlayerTransform.cs(20,44):
error CS1525: Unexpected symbol
)' > Assets/Scripts/PlayerTransform.cs(28,44): > error CS1525: Unexpected symbol
)'
So sorry, I didn't check before posting I was in a rush. Use parenthesis on gameObject.transform.GetChild(0); I was being dumb and I put brackets. I'll edit my answer
Fixed! Now the only error I'm getting is for: Assets/Scripts/PlayerTransform.cs(17,12): error CS1502: The best overloaded method match for UnityEngine.Input.GetButtonDown(string)' has some invalid arguments
Assets/Scripts/PlayerTransform.cs(17,34): error CS1503: Argument #1' cannot convert
UnityEngine.$$anonymous$$eyCode' expression to type `string' (Sorry about the messy text, not used to the syntax on here)
Your solution if I'm correct inables him to store info on the gameobject as you're pulling a fresh prefab everytime, resetting all current values on the previous gameobject
which is good if the gameobject he's instantiating needs different components and has different child objects than the other prefab. Everything can be set in code and adjusted in code if need be.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152604im_/https://answers.unity.com/themes/thub/images/avi.jpg)