I am working through Ryan Creighton’s “Unity3D Game Development By Example” and I’m stuck on the “Robot Repair” example for chapter 5. I’ve been translating the code to C# rather well up to a point. The sample requires that the Card class inherit System.Object and then later this class be attached to the GameScreen. However, you can’t attach scripts that do not inherit MonoBehaviour. There are several stipulations involving “naming the file the same as the class name” and essentially, never using constructors when writing in C#. After quite a bit of wrestling, I’ve managed to get it to run by using 2 files (although Im not sure this is the ideal approach) but I’m still not seeing the game screen. In fact, I don’t think OnGUI is being called. You’ll see in “Card.cs” I do use a constructor but that’s how I figured out how to assign the “img” variable. Otherwise, I WAS using “Awake()” as documentation suggests but then the values were not assigned. I’d like a clear idea of how to use traditional OOP while still utilizing MonoBehavior if that is possible. Thanks. Here is my code:
GameScript.cs
using UnityEngine;
using System.Collections;
public class GameScript: MonoBehaviour {
int rows=4;
int cols=4;
int totalCards=16;
int matchesNeededToWin=8;
int matchesMade=0;
int cardH=100;
int cardW=100;
Card[] aCards; //all cards
Card[,] aGrid; //keeps track of shuffled, dealt cards
ArrayList aCardsFlipped; //2 cards flipped over
bool PlayerCanClick;
bool PlayerHasWon=false;
void Start ()
{
PlayerCanClick=true;
aCards=new Card[totalCards];
aGrid=new Card[4, 4];
aCardsFlipped=new ArrayList();
for(int i=0; i<rows; i++)
{
//aGrid[,] = new Card[4][]; // Create a new, empty array at index i
for(int j=0; j<cols; j++)
{
aGrid[i,j] = new Card();
//Debug.Log(aGrid[i,j].img);
}
}
}
public GUISkin customSkin;
void OnGui()
{
GUI.skin=customSkin;
GUILayout.BeginArea (new Rect (0,0,Screen.width,Screen.height));
BuildGrid();
GUILayout.EndArea();
Debug.Log("yay");
}
void BuildGrid()
{
GUILayout.BeginVertical();
for(int i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
for(int j=0; j<cols; j++)
{
Card a_card = new Card();
a_card=aGrid[i, j];
if(GUILayout.Button((Texture2D)Resources.Load(a_card.img),GUILayout.Width(cardW)))
{
//Debug.Log(a_card.img);
Debug.Log(cardW);
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
}
Card.cs
using UnityEngine;
using System.Collections;
public class Card : System.Object {
bool isFaceUp;
bool isMatched;
public string img;
public Card () //awake is always called before Start
{
isFaceUp=false;
isMatched=false;
img="robot";
}
// Update is called once per frame
void Update () {
}
}