- Home /
Problems changing sprite with code through multiple scripts please help!!,Problems linking C# code to Sprite Renderer
Hello I am trying to change sprites from C# code on mouse press. It is working up to a point. I have one script that four other scripts access. There are four game objects and the player must click them in the right order for the sprites that look a fruit cut into two three four and five pieces to appear. Each script works perfectly until the last one which is identical to the first three, I have no idea why it wont work! Hopefully someone else will see the problem if I post this here. Thankyou so much for your time! I will post the scripts below I cant figure out how to attach them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cuttingbread1 : MonoBehaviour {
public static bool CutOneAvaliable;
public static bool CutTwoAvaliable;
public static bool CutThreeAvaliable;
public static bool CutFourAvaliable;
public SpriteRenderer SR;
public Sprite number1;
public Sprite number2;
public Sprite number3;
public Sprite number4;
public Sprite number5;
public Sprite number6;
public static bool SliceOneClicked = false;
public static bool SliceTwoClicked = false;
public static bool SliceThreeClicked = false;
public static bool SliceFourClicked = false;
public static bool CuttingDone = false;
public float distance = 10f;
public static bool OnMouseDra = false;
public static bool CutOneDone = false;
public static bool CutTwoDone = false;
public static bool CutThreeDone = false;
public static bool CutFourDone = false;
void Start () {
CutOneAvaliable = false;
CutTwoAvaliable = false;
CutThreeAvaliable = false;
CutFourAvaliable = false;
OnMouseDra = false;
CuttingDone = false;
//CutOneDone = false;
// CutTwoDone = false;
// CutThreeDone = false;
// CutFourDone = false;
}
// Update is called once per frame
void Update () {
if (CuttingDone == false) {
OnMouseDra = false;
}
}
void cutting(){
if (Input.GetMouseButtonDown (0)) {
CutOneAvaliable = true;
Debug.Log ("CutOneAvaliable");
SR.sprite = number2;
}
if (SR.sprite == number2) {
CutTwoAvaliable = true;
//CutOneDone = true;
}
if (Input.GetMouseButtonDown (0) && CutTwoAvaliable) {
SR.sprite = number3;
}
if (SR.sprite == number3) {
CutThreeAvaliable = true;
}
if (Input.GetMouseButtonDown (0) && CutThreeAvaliable) {
SR.sprite = number4;
}
if (SR.sprite == number4) {
CutFourAvaliable = true;
}
if (Input.GetMouseButtonDown (0) && CutFourAvaliable) {
SR.sprite = number5;
}
if (SR.sprite == number5) {
CutFourAvaliable = true;
}
}
void OnMouseOver(){
if (Input.GetMouseButtonDown (0)) {
Debug.Log ("enter1");
if (SliceOneClicked == false) {
//Debug.Log ("setsprite2");
SR.sprite = number2;
SliceOneClicked = true;
}
}
}
public SpriteRenderer SR;
public Sprite number5;
public Sprite number4;
// Use this for initialization
void Start () {
GameObject SLICEAREA1 = GameObject.Find("SLICEAREA1");
cuttingbread1 cuttingbread1 = SLICEAREA1.GetComponent<cuttingbread1>();
cuttingbread1.SliceFourClicked = false;
cuttingbread1.CutFourAvaliable = false;
//cuttingbread1.CutThreeDone = false;
//cuttingbread1.CutFourDone = false;
}
// Update is called once per frame
void Update () {
if (cuttingbread1.CutFourAvaliable && Input.GetMouseButtonDown(0)){
SR.sprite = number5;
}
}
void OnMouseOver(){
if (Input.GetMouseButtonDown (0)) {
if (SR.sprite == number4 && cuttingbread1.SliceFourClicked == false ){
Debug.Log ("setsprite5COULDHAPPEN");
SR.sprite = number5;{
Debug.Log ("DID HAPPEN");
}
cuttingbread1.CutFourAvaliable = true;
}
}
}
}
bro have you ever heard of arrays? use them..
public static bool CutOneAvaliable;
public static bool CutTwoAvaliable;
public static bool CutThreeAvaliable;
public static bool CutFourAvaliable;
public Sprite number1;
public Sprite number2;
public Sprite number3;
public Sprite number4;
public Sprite number5;
public Sprite number6;
can be:
public static bool[] CutsAvailable[];
public Sprite[] Numbers;
This code is far too messy, how can u even read it yourself.
Thanks, I actually didn't realize that was a solution I've been $$anonymous$$ching myself and it hasn't been too long since I've started.
I don't see any code initializing SR, so I am assu$$anonymous$$g you are dragging the spring into the SR field in the inspector? If so, are you sure you drug it into the field on this 4th object?
One thing you can do, assu$$anonymous$$g that the script is attached to the object whose SpriteRenderer you want to use, would be to add the following line to your Start routine...
SR = GetComponent<SpriteRenderer>();
With that in Start, SR will get a reference to the SpriteRenderer of the game object the script is attached it.
Hope this helps, -Larry
Hi Larry,
Thanks so much for replying. I was dragging the SpriteRenderer in in the inspector, since the script is not attached to the gameobject with the SpriteRenderer, but that seems to be where the problem is. I have dragged it in to this script in the inspector also so I'm not sure what I've overlooked. Its so bizarre to me since the exact same code on three other objects is connected to the Sprite Renderer and working well.
$$anonymous$$y best recommendation at this point is to debug the code stepping through each line as it is executed, and see where the logic is breaking down. I tried to trace the logic in the code you pasted but ended up with a headache :) Not knocking you, you are just learning, you will get there.
If you are using Visual Studio to write your code, you can debug by putting a break point in the code, by clicking on the extreme left of the line, or by pressing F9 while the cursor is in the line, then click Attach to Unity at the top. Once it is running, launch your game. When it gets to the line you placed the breakpoint on, unity will stop, and Visual Studio will activate, and you will be able to hover your mouse over the variables in your routine, to see their current values, and step through the code line by line, until you see something that isn't working like you expect. This will help you figure out where it is going wrong.
I have not used $$anonymous$$onoDevelop but I assume the debugging is very similar.
Sorry I am not more help with this. -Larry
Your answer
Follow this Question
Related Questions
(C#) Help with Spriterenderer.sprite 2 Answers
Texturing custom 2d sprite 0 Answers
Unity streching sprite gameobject to fit two positions. 1 Answer
Scrolling sprite image 0 Answers
Multiple Cars not working 1 Answer