0
\$\begingroup\$

So I'm trying to write a script in Unity where, when right clicking while the mouse is being followed (I changed it so the cursor is replaced instead, but that's off-topic), that the cursor will change again to a specific texture.

However, for this to work, I need the "color value" variable to have a specific value... but it keeps returning as null in the Update method. Any help on how to get Update to know what the current crayonValue is?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ColoringScript : MonoBehaviour
{
    // Variables
    public GameObject brownCrayon;
    public GameObject selectedShape;
    public Sprite uncolored;
    public Sprite colored;
    public Texture2D brownUnflipped;
    public Texture2D brownFlipped;
    public CursorMode cursorMode = CursorMode.ForceSoftware;
    private GameObject activeCrayon;
    private string crayonValue;
    private string activeTag;
    private bool isFollowingMouse = false;
    private bool isFlipped = false;
    private Vector2 startPos;
    private Vector2 pos;
    private Vector2 movePos;
    private Vector2 hotSpot = Vector2.zero;
    private Image activeImage;
    private Texture2D cursorTexture;

    void Update()
    {
        if(isFollowingMouse)
        {
            if(Input.GetMouseButtonDown(1))
            {
                Debug.Log(crayonValue);
                WasFlipped(crayonValue); // Calls the WasFlipped function to flip the crayon
            }
            if(Input.GetMouseButtonDown(0))
            {
                isFollowingMouse = false; // Sets the boolean so the object stop following the mouse
                activeImage.enabled = true; // Makes the current crayon visible again
                activeCrayon = null; // Empties the activeCrayon variable
                activeImage = null; // Empties the activeRenderer variable
                activeTag = null; // Empties the activeTag variable
                crayonValue = null; // Empties the craonValue variable
                cursorTexture = null; // Reverts the cursor texture
                Cursor.SetCursor(cursorTexture, hotSpot, cursorMode); // Returns the cursor to its default state
            }
        }
    }

    // WasClicked is called when a crayon is left-clicked
    public void WasClicked(string crayonValue)
    {
        switch(crayonValue)
        {
            case "brown":
                activeCrayon = brownCrayon; // Sets the activeCrayon variable to the brown crayon
                activeImage = activeCrayon.GetComponent<Image>(); // Sets the activeRenderer variable
                activeImage.enabled = false; // Makes the crayon invisible
                activeTag = "brown"; // Sets the variable activeTag for ease when clicking a shape
                cursorTexture = brownUnflipped; // Sets the cursorTexture value to the brown crayon default side
                break;
        }
        isFollowingMouse = true; // Sets the boolean so the code knows the cursor is being followed
        Cursor.SetCursor(cursorTexture, hotSpot, cursorMode); // Changes the cursor to simulate picking up the crayon
    }

    // WasFlipped is called when an active crayon is right-clicked
    public void WasFlipped(string crayonValue)
    {
        switch(crayonValue)
        {
            case "brown":
                if(!isFlipped)
                {
                    Debug.Log("flipped brown");
                    isFlipped = true; // States that the crayon is currently flipped
                    cursorTexture = brownFlipped; // Flips the crayon by changing the cursor
                }
                if(isFlipped)
                {
                    isFlipped = false; // States that the crayon is currently being flipped back to normal
                    cursorTexture = brownUnflipped; // Flips the crayon back to its original side
                }
                break;
        }
        Cursor.SetCursor(cursorTexture, hotSpot, cursorMode); // Changes the cursor to simulate flipping the crayon
    }

    // WasColored is called when a shape is clicked
    public void WasColored()
    {
        Debug.Log("Colored" + crayonValue);
        selectedShape.tag = activeTag; // Sets the tag of the shape to the currently active tag
        selectedShape.GetComponent<Image>().sprite = colored; // Colors the shape by changing the source image
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Where does your code assign a non-null value to the crayonValue variable? The only assignment we can see in this code sets it to null, so it's not clear when or how you expect it to take on a value other than that. \$\endgroup\$
    – DMGregory
    Feb 22, 2023 at 15:39

1 Answer 1

0
\$\begingroup\$

It looks like you forgot to save the value of crayonValue from the argument you got in WasFlipped/WasClicked into your member variable of the same name.

Despite using the same name, these are three separate variables. One is a private member variable accessible to any method in the class, and the others are temporary variables declared as method parameters — they cease to exist as soon as execution leaves those methods.

If you want to copy the value from one to the other for safe keeping between calls, write something like this:

public void WasClicked(string crayonValue)
{
    this.crayonValue = crayonValue;

    // ...continue with the rest of the method.

This takes the value from the temporary variable used as a method parameter and stores that value in the private member variable with the same name (using this. to disambiguate the two and say "I want the one that's a member variable that's part of this class instance").

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .