- Home /
[Unity 2d|C#] Help with changing a sprite by holding down a key?
Hello all, currently I am writing a C# script that basically changes a gameObject's sprite to another sprite when a key is held down. I've managed to narrow down all of my mistakes instead of one, and I just cannot figure it out.
The error is this: Assets/shapeChangeScript.cs(21,40): error CS0118: UnityEngine.SpriteRenderer.sprite' is a
property access' but a `type' was expected
And my script is below: using UnityEngine; using System.Collections;
public class shapeChangeScript : MonoBehaviour {
public Sprite Circle;
public Sprite Square;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey ("left")) {
SquareChange ();
}
}
void SquareChange (){
if (SpriteRenderer.sprite == Circle){
SpriteRenderer.sprite spriterend = Square; }
else{
spriterend = Circle;
}
}
}
Please note that I am a beginner at writing code.
Answer by HarshadK · Dec 29, 2014 at 08:29 AM
Your code block:
void SquareChange (){
if (SpriteRenderer.sprite == Circle){
SpriteRenderer.sprite spriterend = Square; }
else{
spriterend = Circle;
}
}
should be:
void SquareChange (){
if (SpriteRenderer.sprite == Circle){
SpriteRenderer.sprite = Square; }
else{
SpriteRenderer.sprite = Circle;
}
}
Aha! This was actually my original script, but I changed it as I was given three errors all saying that an object reference is required to access non-static member 'unityengine.spriterenderer.sprite'. I have changed it to what you specified and the issue still occurs.
Yeah, there is also one more thing you need to do and that is to get a reference to the SpriteRenderer component.
Just do this in Start like:
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
and now your code should be:
void SquareChange (){
if (spriteRenderer.sprite == Circle){
spriteRenderer.sprite = Square; }
else{
spriteRenderer.sprite = Circle;
}
}
notice how the SpriteRenderer in this code is changed to the reference which is spriteRenderer.
The lines in which these errors referred to are 20,21 and 23.
Oh gosh. I've done this, and yet more errors replace the previous ones xd. Four errors each saying that the name 'spriteRenderer' does not exist in the current context. Here is my full code:
using UnityEngine;
using System.Collections;
public class shapeChangeScript : $$anonymous$$onoBehaviour {
public Sprite Circle;
public Sprite Square;
// Use this for initialization
void Start () {
spriteRenderer = GetComponent<SpriteRenderer> ();
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ey ("left")) {
SquareChange ();
}
}
void SquareChange (){
if (spriteRenderer .sprite == Circle){
spriteRenderer .sprite = Square; }
else{
spriteRenderer .sprite = Circle;
}
}
}
Thank you so much for your patience.
Answer by mattyman174 · Dec 29, 2014 at 08:21 AM
SpriteRenderer.sprite spriterend = Square;
This line makes no sense.
Are you trying to initialize the spriterend variable as a type of SpriteRenderer.sprite??
If so, SpriteRenderer.sprite is not a type, it is a property of SpriteRenderer.
You need to give spriterend a valid type.
I'm not quite sure what you been by this.. the reason I added 'spriterend' was to use a name that wasn't the name of a class, and doing this solved three other errors.
Please bear with me, I'm trying very hard to understand.
Okay, that seemed to have done the trick! But now I have two more errors, the first one saying: Assets/shapeChangeScript.cs(20,36): error CS0120: An object reference is required to access non-static member UnityEngine.SpriteRenderer.sprite' And the second one saying: Assets/shapeChangeScript.cs(23,25): error CS0103: The name
spriterend' does not exist in the current context
The latter I kind of understand, meaning that the term 'spriterend' hasn't been mentioned anywhere else, however I know not how to fix it.
Check the answer above this one, its probably what you were intending more than the answer i gave.
Your answer
Follow this Question
Related Questions
How do I make sprite renderer's sprite change in runtime c#? 0 Answers
Multiple Cars not working 1 Answer
Texturing custom 2d sprite 0 Answers
Distribute terrain in zones 3 Answers
2D platformer- getting errors I don't understand (c#) 1 Answer