Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by laurenmhowie · Dec 25, 2017 at 08:23 PM · c#spritespriterenderer

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;
             }
 
         
 
 
     }
 
 }
 
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Dec 25, 2017 at 09:08 PM 0
Share

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.

avatar image laurenmhowie ShadyProductions · Dec 26, 2017 at 04:26 AM 0
Share

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.

avatar image Larry-Dietz · Dec 25, 2017 at 10:18 PM 0
Share

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

avatar image laurenmhowie Larry-Dietz · Dec 26, 2017 at 04:29 AM 0
Share

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.

avatar image Larry-Dietz laurenmhowie · Dec 26, 2017 at 04:44 AM 0
Share

$$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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

428 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges