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
}
}
crayonValuevariable? 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\$