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 "private SpriteRenderer" issues with coding

Discussion in 'Scripting' started by zo8j2020, Jun 18, 2020.

  1. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    This is my first time ever using Unity so apologies for the lack of proper terms. ^^;

    I was in the middle of using this tutorial to create a rhythm game prototype:
    and reached the point around 2:23 where I was supposed to write "private SpriteRenderer theSR;" [and so on]. However, when I did my script didn't seem to recognise the term SpriteRenderer [it didn't turn green like in the video, once again apologies for the lack of knowhow!] and it didn't do anything when I played the full script.

    Any advice on what I could do? I'd appreciate any help, thank you!
    ZJ
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    zo8j2020 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    zo8j2020 likes this.
  4. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Thanks PraetorBlue and Kurt, however unfortunately that doesn't seem to have fixed the problem. This is the code I have at the moment in my Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     private SpriteRenderer theSR;
    8. // ^ there is a screwdriver symbol and yellow line next to this line above//
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.      
    21.     }
    22. }
    23.  
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Hmmm I don't see anything wrong with the script itself. Which version of Unity and Visual Studio are you using?
     
  6. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    I am using Visual Studio 2019 and Unity 2019.4.1f1 (64-bit) Personal, if that helps.
     
  7. Yellow and screwdriver usually just warning. Probably it wants to tell you to rename your member to theSr or something.
    If you hover your mouse over the yellow line it will tell you what is it about.

    Always remember:
    - red is error
    - yellow is warning (most of the time it can be safely ignore)
     
    PraetorBlue likes this.
  8. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Ah, thank you so much for letting me know, never knew that!
     
  9. if you switch back to Unity and see some red errors in the console, then you have an error for sure, and that is always important, because ultimately Unity will compile your scripts
     
  10. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Thanks - I probably sound stupid asking this, but what do you mean by "switch back to unity"? Do you mean running the game with the code visible in a separate tab or something similar?
     
  11. You have the project open in Unity. You open up a script to edit. You press the ALT-TAB to go back to Unity. At this point Unity compiles your code and will throw an error if something is wrong. You don't have to play your game to compile the code. (Obviously you will have to play it to validate if what you have done is right or not)
     
  12. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    I see. I pushed ALT-TAB and not much happened, so I assume it's still alright to try and continue coding? I'm still not too sure if the SpriteRenderer not being recognised is an issue or not...
     
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    If it wasn't being recognized, you would be getting an error. There's no error, so you're good to go.
     
  14. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Thank you so much for helping me out, you all!
     
    Kurt-Dekker likes this.
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Also, just wanted to add: WELCOME... you have a wonderful journey ahead of you, Unity is the funnest, most-bestest game engine ever. At times it will be baffling or confusing, but this applies to anything complex. Stick with it, learn the terminology, and head back here whenever you have pointed questions about what you're seeing.

    Also, work through lots of really different tutorials, and take your time. Try not to proceed until you actually understand a section of a tutorial, but don't linger too long if it truly still seems confusing. Move forward and try to "back connect" it in your brain.
     
    zo8j2020 likes this.
  16. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Hello again...

    Unfortunately I got to a point where Unity was having trouble with recognizing my code. The person in the video was using this code to make it so that the buttons onscreen would react to keys pressed accordingly:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonController : MonoBehaviour
    6. {
    7.     private SpriteRenderer theSR;
    8.     public Sprite defaultImage;
    9.     public Sprite pressedImage;
    10.  
    11.     public KeyCode keyToPress;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         theSR = GetComponent<SpriteRenderer>();
    17.    
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (input.GetKeyDown(keyToPress))
    24.         {
    25.             theSR.sprite = pressedImage;
    26.         }
    27.  
    28.         if(input.GetKeyUp(keyToPress))
    29.         {
    30.             theSR.sprite = defaultImage;
    31.         }
    32.     }
    He later added the script to each button and was able to change the Image and keyToPress of each, yet despite my code being identical, those options did not appear for me.

    I can provide screenshots of both my work and his own if that could help! Thank you for your time once again.

    Edit: I also received these errors after completion: Annotation 2020-06-19 002524.png
     
    Last edited: Jun 19, 2020
  17. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Capitalization matters in C# code. Your code is not identical, because you didn't properly capitalize "Input".
     
  18. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Sorry, I didn't realise that! It's working perfectly now, thank you so much! <3
     
    PraetorBlue likes this.
  19. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Unfortunately I have run into another problem.

    I was following the tutorial above and had created a script called GameManager that would play both the music and start the notes scrolling down when a key is pressed.

    However, because of that I had to omit a part of the script from the scrolling note script (which was fine at the time, the notes wouldn't scroll until a key was pressed) and I did, and added the necessary code to the GameManager.

    However, now when I press "play" the notes automatically scroll down and the music only plays once I press a key!

    Below is the scrolling note code;
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class BeatScroller : MonoBehaviour
    7. {
    8.     public float beatTempo;
    9.  
    10.     public bool hasStarted;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         beatTempo = beatTempo / 60f;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.        /* if (!hasStarted)
    23.         {
    24.             if (Input.anyKeyDown)
    25.             {
    26.                 hasStarted = true;
    27.             } */ [This part has been omitted due to the GameManager script having something similar]
    28.        
    29.        
    30.        
    31.             transform.position -= new Vector3(0f, beatTempo * Time.deltaTime, 0f);
    32.         }
    33.  
    34.  
    35.     }
    36.  

    And here is the GameManager code;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public AudioSource theMusic;
    8.  
    9.     public bool startPlaying;
    10.  
    11.     public BeatScroller theBS;
    12.  
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(!startPlaying)
    25.         {
    26.             if(Input.anyKeyDown)
    27.             {
    28.                 startPlaying = true;
    29.                 theBS.hasStarted = true;
    30.  
    31.                 theMusic.Play();
    32.             }
    33.         }
    34.     }
    35. }
    36.  
    Once again, all help is greatly appreciated and thank you for your time! <3
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    The first answer is, "back up and go un-omit it." :)

    Seriously, Unity isn't just code. How the code is connected and set up in the inspector and how the guy writing the tutorial wanted to set it all up are equally important. I have no idea about that and I'm not going to go do the tutorial.
     
    zo8j2020 likes this.
  21. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Honestly, Kurt. You have been a lifesaver for me recently! Thank you, truly! It worked perfectly fine after following your advice.
     
    Kurt-Dekker likes this.