• 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 BlacKatFever · Sep 27, 2011 at 10:05 AM · c#

Irritating JavaScript to C# conversion

Hey all,

I'm sitting here trying to convert Ryan Creighton's (Unity 3D Game Development By Example for Beginners) "Robot Repair" game into C# since that is the programming language that my boss asked me to learn and I've been stuck here for quite some time.

The three errors I'm currently getting are -

(98,53) Type string[]' does not contain a definition for RemoveAt' and no extension method `RemoveAt' of type string [] (are you missing a using directive or an assembly reference?)

The other two errors are at 102 / 105's aCards.Add(card); saying that there is no definition / extension method for Add.

I've tried dropping a ArrayList here and using List so I wouldn't have to mess with typecasting as well as changing the way I write everything but I just don't know enough I guess.

The original code up in JS is (up to this point):

 class Card extends System.Object
  var cols:int = 4; //the number of cols in the grid 
  var rows:int = 4; //the number of rows in the grid
  var totalCards:int = col*rows;
  var matchesNeededToWin:int = totalCards * 0.5;
  var matchesMade:int = 0;
  var cardW:int = 100;
  var cardH:int = 100;
  var aCards:Array; //Store all the cards we create here
  var aGrid:Array; //Keep track of shuffled, dealt cards
  var aCardsFlipped:ArrayList; //Store the two cards the player flips over
  var playerCanClick:boolean;
  var playerHasWon:boolean;

 function Start()
 {
  playerCanClick = true;
 //Initialize Arrays as empty lists
  aCards = new Array();
  aGrid = new Array();
  aCardsFlipped = new ArrayList();

  BuildDeck // my problem area :(

  for(i=0; i<rows; i++)
  {
   aGrid[i] = new Array();
  for(j=0; j<cols; j++)
  {
   aGrid[i][j] = new Card();
 }
 }
 }

  function OnGUI()
 {
  GUILayout.BeginArea (Rect (0,0,Screen.width, Screen.height));
  BuildGrid();
  GUILayout.EndArea();
 }

  function BuildGrid()
 {
  GUILayout.BeginVertical();
  GUILayout.FlexibleSpace();
   for (i=0; i<rows, i++)
    {
     GUILayout.BeginHorizontal();
     GUILayout.FlexibleSpace();
     for(j=0; j<cols; j++)
      {
       var card:Object = aGrid[i][j];
       if (GUILayout.Button(Resources.Load(card.img),
        GUILayout.Width(cardW)))
       {
        Debug.Log(card.img);
       }
      }
      GUILayout.FlexibleSpace();
      GUILayout.EndHorizontal();
     }
     GUILayout.FlexibleSpace();
     GUILayout.EndVertical();
    }

  function BuildDeck()
 {
  var totalRobots:int = 4;
  var card:Object; // stores reference to a card

  for (i=0; i<totalRobots; i++)
   {
    var aRobotsParts:Array = ["Head", "Arms", "Leg"];
    for (j=0; j<2; j++)
     {
      var someNum:int = Random.Range(0, aRobotParts.length);
      var theMissingPart:String = aRobotParts[someNum];

      aRobotParts.RemoveAt(someNum);

      card = new Card("robot" + (i+1) + "Missing" + theMissingPart);
      aCards.Add(card);

      card = new Card("robot" + (i+1) + theMissingPart);
      aCards.Add(card);
     }
   }
 }

  
    
 
  var isFaceUp:boolean = false;
  var isMatched:boolean = false;
  var img:String;

  function Card(img:String)
 {
  this.img = img;
 }
 }


Is as far as I've gotten. The basic idea is to create a grid of cards distributed randomly. The player then flips over cards to find pairs like in Memory.

This is as far as I've gotten.

 using UnityEngine;

using System.Collections; using System.Collections.Generic;

public class GameScript : MonoBehaviour {

 public int cols = 4;    // the number of columns in the card grid
 public int rows = 4;    // the number of rows in the card grid
 public int totalCards = 16;
 private int matchesNeededToWin = 8;
 private int matchesMade = 0;
 private int cardW = 100;
 private int cardH = 100;
 
 Card[] aCards;                // We'll store all the cards we create in this Array                    
 Card[,] aGrid;                 // This array will keep track of the shuffled, dealt cards.
 ArrayList aCardsFlipped = new ArrayList();
 Card card = new Card();
 
 private bool playerCanClick;
 private bool playerHasWon = false;
 
 
 
 // Use this for initialization
 void Start () {
     
     playerCanClick = true;
     aCards = new Card[totalCards];
     aGrid = new Card[rows,cols];
     aCardsFlipped = new ArrayList();
     
     
     for (int i = 0; i < rows; i++){
                     
         for (int j = 0; j < cols; j++){
             aGrid [i,j] = new Card();
         }
     }
     BuildDeck();
 }
 
 // Update is called once per frame
 void Update () 
 {
 }
 
 void OnGUI ()
     {
         GUILayout.BeginArea (new Rect (0, 0, Screen.width, Screen.height));
         BuildGrid();
         GUILayout.EndArea();
         
 }
     
 void BuildGrid ()
     {
      GUILayout.BeginVertical();
      GUILayout.FlexibleSpace();
         for (int i=0; i<rows; i++)
          {
           GUILayout.BeginHorizontal();
           GUILayout.FlexibleSpace();
             for (int j = 0; j<cols; j++)
             {
                 //Card card = new Card();
                 card = aGrid [i,j];
                 
                 if (GUILayout.Button((Texture2D)Resources.Load(card.img),
                     GUILayout.Width (cardW)))
                 {
                     Debug.Log (cardW);
                 }
             }
           GUILayout.FlexibleSpace();
           GUILayout.EndHorizontal();
          }    
       GUILayout.FlexibleSpace();    
       GUILayout.EndVertical();
 }
             
  void BuildDeck ()
     {
           int totalRobots = 4;    // there are four robots
         Card card;
         
     
         for (int i=0; i<totalRobots; i++)
         {
                 
             string[] aRobotParts = {"Head","Arm","Leg"};
         
             for (int j=0; j<2; j++)
             {
                 int someNum = Random.Range (0,totalRobots);
                 string theMissingPart = (string)aRobotParts[someNum];
                 
                 aRobotParts.RemoveAt(someNum);
             
                 card = new Card("robot" + (i+1) + "Missing" + theMissingPart);
                 aCards.Add(card);
                 
                 card = new Card ("robot" + (i+1) + theMissingPart);
                 aCards.Add(card);
             }
         }
     }
 }        
 public class Card : System.Object {
 
     private bool isFaceUp = false;
     private bool isMatched = false;
     public string img;
     
     public Card(){
         img = "robot";
     }
     public Card(string str){
         this.img = str;
         }
     }
 


I apologize if there is some rule against posting code out of books. If so, I will remove the original author's code. I'm sorry for posting so much but I've been banging my head on the desk for DAYS! I know there is probably some other way I could have done it but I know there should be a way to write the same thing in C# and I'll need to get that knowledge under my belt one day. No one here knows C# - they're too busy being profound in C++ and whatnot.

Thanks for any help you can give. I'd really appreciate it.

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
3
Best Answer

Answer by syclamoth · Sep 27, 2011 at 10:18 AM

Instead of just using an array of cards, try using a generic list, instead!

 // Put this at the top
 using System.Collections.Generic


 List<Card> aCards = new List<Card>();

Then all the missing functionality should work again.

Comment
Add comment · Show 2 · 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 Bunny83 · Sep 27, 2011 at 10:58 AM 2
Share

Yep, don't forget that the Unityscript(JS) "Array" class is NOT a native array. Native array look exactly the same in Unityscript:

 // C#
 Card[] aCards;
 
 // Unityscript
 var aCards : Card[];

But native arrays can't change their size at runtime, so you need to use a container class like List ins$$anonymous$$d.

btw. the Unityscript Array class is the same as the ArrayList class in C#, but i would recommend to use a generic List ;)

avatar image BlacKatFever · Sep 27, 2011 at 12:28 PM 0
Share

Thank you.

I'd been messing around with Lists for a while but trying to pass them off as ( List [String] ). I will give this a shot. I'd nearly given up hope. So frustrating knowing that there is an answer out there but not being able to find it. I was starting to doubt I'd ever be able to $$anonymous$$ch myself the language.

Thanks again! I appreciate the suggestion!

I'm going to leave this question open for now so I can try this and post my results.

Bunny,

I didn't know that the UnityScript Array was the same as the ArrayList is C#:P So much stuff out there to learn! :P

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Temporarily disable gamepad button 1 Answer

Play specific animation in mechanim 1 Answer

Script from JS to 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