Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How do I create "Two texture" or "Two state" blocks with a condition?

Discussion in '2D' started by Proxima_Centauri, Oct 21, 2020.

  1. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    So, I'm trying out mechanics to a game, and I thougt of a "Gravity changer" level, where you take an item and the gravity is inverted.

    For that, I want the blocks/ground in the scene to change color when the gravity is inverted. For that, I created two tilesets (It's a pixel art-esque game, so I'm creating empty objects as ground, where I add the collider, and add children to the ground object with just the Sprite renderer with the correct tile, this will be important later), one blue (for normal gravity, downGround) and one red (for inverted gravity, upGround). I also added a public boolean variable to my Character controller script to know when is the gravity inverted.

    I thought about using the animator, but since I'm using two tilesets, it means I'd have to animate every single tile to change from one state to another, and I'm sure there must be a better way.

    What I did was creating two ground objects (one with the downGround tileset and other one with the upGround tileset), and adding those as children into another ground object where I actually add the collider and the script that will manage the ground changing. At the beginning of the program, the upGround is disabled by default, so I figured out I needed to toggle the enable and disable of both grounds so they change (so, if gravity is inverted, downGround will be disabled instead and upGround enabled, and vice versa). For that I used this code:

    Code (CSharp):
    1. public class GravityTileSwitch : MonoBehaviour
    2. {
    3.     private PlayerController player;
    4.     public GameObject downGround, upGround;
    5.        
    6.     void Start()
    7.     {
    8.         //Get PlayerController component public variables access
    9.         player = GetComponent<PlayerController>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         //Show upGround
    15.         if (player.invGravity)
    16.         {
    17.             downGround.SetActive(false);
    18.             upGround.SetActive(true);
    19.         }
    20.         //Show downGround
    21.         else if (!player.invGravity)
    22.         {
    23.             downGround.SetActive(true);
    24.             upGround.SetActive(false);
    25.         }
    26.     }
    27. }


    In the public GameObject variables I attach the corresponding object, downGround and upGround.

    The problem is that, when I run the program, not only is the ground not changing, but the console displays an error that says


    NullReferenceException: Object reference not set to an instance of an object GravityTileSwitch.Update () (at Assets/Scripts/GravityTileSwitch.cs:19)

    Does somebody know why is my code not working? I'd appreciate a lot your help, thank you!
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    change this to public so you can see it in the inspector

    Code (CSharp):
    1. private PlayerController player;
    it looks like its not getting assigned
     
    Proxima_Centauri likes this.
  3. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    I tried it, and attached my Player Object into the inspector (since it's the object that is managing my gravity bool), but when I press play to try it out the error keeps appearing.

    I checked the inspector while In-game, and it turns out that it disappeared (the slot says "None (PlayerController) instead of "Player(P.C.)"), and when I stop it and check it, the script is still attached there
     
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    do you have a playercontroller attached to the same object as gravitytileswitch? if not thats the problem
     
    Proxima_Centauri likes this.
  5. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    I don't have it attached, because there is all the control like jumping, moving, etc. Or then I should create a different script just for the Gravity so I can attach that to the ground?
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    well its because when you do this:

    Code (CSharp):
    1.   player = GetComponent<PlayerController>();
    it takes the script that is attached to the object

    since you dont have it attached this is the same as doing:
    Code (CSharp):
    1.   player = null;
    which gives you that null exception. What you want is something like this:

    Code (CSharp):
    1.     public class GravityTileSwitch : MonoBehaviour
    2.     {
    3.         public GameObject playerObj;
    4.         public PlayerController player;
    5.         public GameObject downGround, upGround;
    6.          
    7.         void Start()
    8.         {
    9.             //Get PlayerController component public variables access
    10.             player = playerObj.GetComponent<PlayerController>();
    11.         }
    12.    
    13.         private void Update()
    14.         {
    15.             //Show upGround
    16.             if (player.invGravity)
    17.             {
    18.                 downGround.SetActive(false);
    19.                 upGround.SetActive(true);
    20.             }
    21.             //Show downGround
    22.             else if (!player.invGravity)
    23.             {
    24.                 downGround.SetActive(true);
    25.                 upGround.SetActive(false);
    26.             }
    27.         }
    28.     }
    29.  
    player obj is the game object that has your player controler script


    or you can just remove

    player = playerObj.GetComponent<PlayerController>();

    from your code and drag it in the inspector
     
    Proxima_Centauri likes this.
  7. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    Thank you! It solved my problem
     
    raarc likes this.