Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KinceP · Feb 14, 2018 at 12:20 AM · platformerswappingmultiple-objects

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.

Comment
Add comment · Show 2
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 PlayCreatively · Feb 14, 2018 at 12:32 AM 0
Share

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?

avatar image KinceP PlayCreatively · Feb 14, 2018 at 12:47 AM 0
Share

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. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 15 · 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 KinceP · Feb 14, 2018 at 01:20 AM 0
Share

@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 )'

avatar image LiamofElites KinceP · Feb 14, 2018 at 01:23 AM 0
Share

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

avatar image KinceP LiamofElites · Feb 14, 2018 at 01:27 AM 0
Share

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)

Show more comments
avatar image PlayCreatively · Feb 14, 2018 at 01:37 AM 0
Share

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

avatar image LiamofElites PlayCreatively · Feb 14, 2018 at 02:13 AM 0
Share

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.

avatar image PlayCreatively LiamofElites · Feb 14, 2018 at 03:39 AM 0
Share

Inables not enables

Show more comments

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

79 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 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 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 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

How to stop previous animation? 1 Answer

Prevent Character from Falling off Edge when more than Half Off (2D) 1 Answer

Player passenger moving when being pushed by two platforms 0 Answers

Unity3D Fog Bug Please Help 0 Answers

Why won't the coin disappear when the player collides with it? 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