Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem with shifting between 2 meshes (?)

Discussion in 'Scripting' started by zRonno, Jun 24, 2020.

  1. zRonno

    zRonno

    Joined:
    Jun 9, 2020
    Posts:
    8
    I'm decently new here, so thanks for any help and hopefully I can make enough sense.
    I had this idea for a mechanic where a player could shift between a "sphere mode" and a "capsule mode". I made some code for this mechanic.
    Code (CSharp):
    1.  void SHIFT()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.LeftShift))
    4.         {
    5.             if (CapsuleShift) //Shift from capsule to sphere
    6.             {
    7.                 meshFilter.mesh = sphereMesh;
    8.                 boxCollider.enabled = !boxCollider.enabled;
    9.                 sphereCollider.enabled = sphereCollider.enabled;
    10.  
    11.                 CapsuleShift = false;
    12.                 SphereShift = true;
    13.             }
    14.             if (SphereShift) //Shift from sphere to capsule
    15.             {
    16.                 meshFilter.mesh = capsuleMesh;
    17.                 sphereCollider.enabled = !sphereCollider.enabled;
    18.                 boxCollider.enabled = boxCollider.enabled;
    19.  
    20.                 SphereShift = false;
    21.                 CapsuleShift = true;
    22.             }
    23.         }
    24.      
    25.     }
    Here's the catch: This doesn't actually work, but when I comment out "meshFilter.mesh = capsuleMesh;", shifting to the sphere form actually does work. Why is this happening?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Follow the logic: you check the key, if it is capsule shift, you set sphereshift true, then line 14 goes "Hey, sphereshift is true now!" You either need to have an else clause, or else return after processing each condition.
     
  3. zRonno

    zRonno

    Joined:
    Jun 9, 2020
    Posts:
    8
    Ah yes, that was it. Don't know how I didn't think that through...

    Thanks for the help!