Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ninjaroy · Dec 29, 2014 at 07:48 AM · c#2dspriterenderer

[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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
      }
  }
Comment
Add comment · Show 8 · Share
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 mattyman174 · Dec 29, 2014 at 08:31 AM 0
Share

This makes more sense.

avatar image Ninjaroy · Dec 29, 2014 at 08:42 AM 0
Share

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.

avatar image HarshadK · Dec 29, 2014 at 08:49 AM 0
Share

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.

avatar image Ninjaroy · Dec 29, 2014 at 08:49 AM 0
Share

The lines in which these errors referred to are 20,21 and 23.

avatar image Ninjaroy · Dec 29, 2014 at 08:55 AM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 5 · Share
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 Ninjaroy · Dec 29, 2014 at 08:25 AM 0
Share

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.

avatar image Ninjaroy · Dec 29, 2014 at 08:25 AM 0
Share

Please bear with me, I'm trying very hard to understand.

avatar image mattyman174 · Dec 29, 2014 at 08:28 AM 0
Share
    Sprite spriterend = Square;

Change it to this.

avatar image Ninjaroy · Dec 29, 2014 at 08:35 AM 0
Share

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.

avatar image mattyman174 · Dec 29, 2014 at 08:41 AM 0
Share

Check the answer above this one, its probably what you were intending more than the answer i gave.

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

27 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

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


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