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 /
  • Help Room /
avatar image
1
Question by TheRichardGamer · Mar 03, 2016 at 08:17 PM · c#errorclasses

How do I check if the current PieceType is a King?

I'm making a chess game and I want to set the icon automatically based on what PieceType I have. Here's what I've been trying to do in the Piece-class:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Piece
 {
 
     //Basics
     public Texture2D icon;
 
     public enum PieceType
     {
         King, Queen, Bishop, Knight, Rook, Pawn
     }
 
     public enum PieceSide
     {
 
         White, Black
 
     }
 
     //Position
     public string StrPos;
     public int NumPos;
 
     public Piece myPiece = new Piece();
 
     public void UpdatePiecePosition(string stringPart, int NumberPart)
     {
 
 
 
     }
 
     public void SetPieceIcon()
     {
 
         if (myPiece.PieceType == PieceType.King) 
         {
 
 
 
         }
 
     }
 
 }
 
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

· Add your reply
  • Sort: 
avatar image
1

Answer by Fydar · Mar 03, 2016 at 08:42 PM

Hi, @TheRichardGamer Add to the piece class: public PieceType type; pubic PieceSide side;

 //Later on in program
 
 if ( (INSERT PIECE).type == Piece.PieceType.King) {
           (...)
 }


I see you have defined your enumerations but haven't used them. This was a problem for me when I first started using them. So your overall script will be:

 [System.Serializable]
  public class Piece
  {
  
      //Basics
      public Texture2D icon;
  
      public enum PieceType {King, Queen, Bishop, Knight, Rook, Pawn}
      public enum PieceSide {White, Black}
      
      public PieceType type;
      public PieceSide side;
       
      //Position
      public string StrPos;
      public int NumPos;
       
      //What is this doing here?
      public Piece myPiece = new Piece();
  
      public void UpdatePiecePosition(string stringPart, int NumberPart)
      {
       
      }
  
      public void SetPieceIcon() {
          if (this.type == PieceType.King) 
          {
                 //THIS IS HOW TO CHECK
          }
      }
  }

Also, please look into Properties for the string position, so you only have to store a number position, here is a link: https://msdn.microsoft.com/en-gb/library/x9fsa0sw.aspx

And for your number position, I recommend you use the data type Vector2, because it will allow you to do things in the future of a chess game which you may find very useful.

I commented a section of the code: //What is this doing here? I done so because you are creating an infinite loop of declarations of the object "Piece", which will crash unity. You also don't have a constructor for "Piece", so add something like this to your program:

 public Piece (PieceType _type) {
      this.type = _type;
 }
Comment
Add comment · Show 3 · 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 TheRichardGamer · Mar 04, 2016 at 02:58 PM 0
Share

Ok, I'm done with the first part. However, about setting the icon... when I try to set the icon as a sprite and apply it to a sprite-renderer, nothing happens. Why? The original textures are certainly PNG but it says that they are Sprites in the inspector.

Here's my code: using UnityEngine; using System.Collections;

 public class $$anonymous$$ing : $$anonymous$$onoBehaviour {
 
     private Piece.PieceSide mySide;
     public Piece king;
     public Sprite myIcon;
 
     void Start ()
     {
 
         if (this.mySide == Piece.PieceSide.White)
         {
 
             king = new Piece (Piece.PieceType.$$anonymous$$ing, mySide, "Icons/white_king.png");
             myIcon = (Sprite)Resources.Load ("Icons/white_king.png");
             this.gameObject.GetComponent<SpriteRenderer> ().sprite = (Sprite)myIcon;
 
         }
 
         if (this.mySide == Piece.PieceSide.Black)
         {
 
             king = new Piece (Piece.PieceType.$$anonymous$$ing, mySide, "Icons/black_king.png");
             myIcon = (Sprite)Resources.Load ("Icons/black_king.png");
             this.gameObject.GetComponent<SpriteRenderer> ().sprite = (Sprite)myIcon;
 
         }
 
     }
 
 }
 
avatar image EmHuynh TheRichardGamer · Mar 04, 2016 at 08:59 PM 0
Share

Hey, @TheRichardGamer. I simplified the $$anonymous$$ing script, it is more clear and efficient.

 public class $$anonymous$$ing : $$anonymous$$onoBehaviour {
      private       Piece.PieceSide    mySide;
      public        Piece              king;
      public        Sprite             myIcon
      
      void Start() {
          string imagePathName = "Icons/" + ( ( mySide == Piece.PieceSide.White )
              ? "white_king.png" : "black_king.png" );
          
          king = new Piece( Piece.PieceType.$$anonymous$$ing, mySide, imagePathName );
          myIcon = ( Sprite )Resources.Load( imagePathName );
          this.gameObject.GetComponent< SpriteRenderer >().sprite = myIcon;
      }
  }

@Fydar 's list of possibilities are right. I can't spot anything that may cause the problem you are having.

avatar image Fydar · Mar 04, 2016 at 08:36 PM 0
Share

I think there are 2 possibilities:

1: The if statements aren't working, have you set the value of "mySide"? It is private so I doubt you have set it in the editor.

2: The Resources.Load isn't setup correctly.

Apart from that, I don't see how it isn't working. Sorry.

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

116 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

Related Questions

Issue with class constructor. 1 Answer

problems with mopub 0 Answers

The body of '' cannot be an iterator block because 'void' is not an iterator interface type??? 2 Answers

Coding Errors 1 Answer

A namespace cannot directly contain mambers such as fields or methods... 2 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