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;
}
}
}
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.
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.