• 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 /
This question was closed Jul 09, 2017 at 06:51 PM by Hellium for the following reason:

Posting about a specific compiling error or NullReferenceException

avatar image
-1
Question by ndever · Jul 09, 2017 at 05:33 PM · spriterenderer

Error CS0201 when trying to render random sprite

Hi,

I am trying to write a really simple script which is supposed to render a random sprite. The first part of the code was working, I was able to assign different sprites through the inspector. Than came the hard part for me. No matter how I try, it always gives me Error CS0201. I know I probably doing some basic stuff wrong here but as a rookie I can't figure out what. Please help me :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RandomDice : MonoBehaviour {
     
     [System.Serializable]
     public class SpriteList
     {
         public Sprite[] sprites;
     }
     public Sprite[] SpriteLists;
     void Start ()
     {
         SpriteRenderer sr = GameObject.GetComponent<SpriteRenderer>();
         sr.sprite = SpriteList[Random.Range(0, 5)];
     }
 }
 
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

1 Reply

  • Sort: 
avatar image
0

Answer by EpsilonQoppa · Jul 09, 2017 at 05:49 PM

You're trying to declare a variable within the constructor.

      public class SpriteList
      {
          public Sprite[] sprites;
      }

Try taking out:

          public Sprite[] sprites;
 
Comment
Add comment · Show 1 · 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 ndever · Jul 09, 2017 at 06:22 PM 0
Share

It doesn't seem to work. However I noticed that after a restart it throws new errors:

Assets/RandomDice.cs(16,15): error CS0118: RandomDice.DiceList' is a type' but a variable' was expected Assets/RandomDice.cs(16,15): error CS0119: Expression denotes a type', where a variable', value' or `method group' was expected

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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

Related Questions

GPU Instancing causes sprites of same material to be rendered on top of each other, regardless of game object position 0 Answers

how can i make this physic 1 Answer

I have an animaiton problem about sprite loading. 0 Answers

My shader doesn't work if I use the same material for multiple objects with the same texture. 1 Answer

Fixed Sprite Size in every monitor Screen 0 Answers


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