Instantiating card deck

Hi,
So I am making a small card game. Nothing fancy, and I decided to instantiate the whole deck at once. The problem is it keeps throwing three errors at me which I can’t resolve. I have searched all over but I (think) I’m writing it the way it should be written. Here is my code:

using UnityEngine;
using System.Collections;

public class Instantiate : MonoBehaviour {

public GameObject cardPrefab;
public GameObject[] cards = new GameObject[107];

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void CreateDeck () {
		for (var i = 0; i<52; i++)
		{
			cards[i] = Instantiate(cardPrefab, Vector3(0, i*0.1, 0), transform.rotation) as GameObject;
		}
	}
}

And here are the errors I am getting:

Any help would be greatly appreciated.

  1. your class name cant be a diff classes name, your calling the class that your in, so rename yourclass to something like cardinstatiate.

Try putting a 'new ’ before Vector3() on line 22.

This is in C# right?
I notice you use “for (var i = 0; i<52; i++)” for your loop.
The ‘var’ command threw me off thinking you were programming in UnityScript!
(As opposed to the more familiar “for (int i = 0; i<52; i++)”)

[Edit]
I think the error is because the ‘Instantiate’ command is expecting a Vector3 value.
In C# you need the 'new ’ to create a value, otherwise you are passing just the Vector3 type definition or something.
However, in UnityScript, the 'new ’ isn’t required, and is implied.

I’m not absolutely sure C# supports the use of ‘var’ in Unity, so that may be another problem. If so, use ‘int’ as suggested.

Unless of course you meant this to be all UnityScript, then you have to convert your other code appropriately.

1 Like

Ahhh nice find, totally didnt see that var! :stuck_out_tongue:

I can’t believe I didn’t pick that up. The place I was getting the info from must have been using unity script. Thanks Laakerules and Slydog for your help, you have been great.

1 Like

I saw this and I was like what? var is not supported in C#. And then realised the post is from 2012. Times have changed.

I saw this and I was like what? var is supported in C#. Times haven’t changed.

2 Likes

:slight_smile: Yes, C# supports var since version 3 (2007) So essentially since Unity 2.0.

1 Like