- Home /
InvalidCastException C#
Hello friends,
I'm working on a breakout clone, and have run into some difficulty with an invalid cast exception. I'm using an Array of GameObjects to store the bricks, so it will be easy to iterate through them and set their strengths, points, colours etc.
The code compiles ok, and the error occurs at runtime. This error is: Invalid Cast Exception: Cannot cast from source to destination type. This error occurs near the bottom of the script, I've marked it out with a comment block.
Can someone point out where the problem is?
Also, if you could, I'd really like an explanation as to whether in scripts, prefabs should be declare as type Transform, or GameObject? I've seen it done as both, but don't understand the rationale. Thanks!
using UnityEngine;
using System.Collections;
public class GameControl : MonoBehaviour
{
/** INSTANCE FIELDS **/
public GameObject brick;
public GameObject [,] brickArray;
// Layout Fields
private float brickStartX;
private float brickStartY;
// Use this for initialization
void Start ()
{
// Instantiate Variables
brickStartX = -9.5f;
brickStartY = 5;
brickArray = new GameObject [5, 12];
// Instantiate Bricks
CreateBricks();
}
// Update is called once per frame
void Update ()
{
}
// Instantiates Bricks at the start of a level
void CreateBricks()
{
float tempBrickStartX = brickStartX;
float tempBrickStartY = brickStartY;
for(int i = 0 ; i < 5 ; i++)
{
for(int j = 0 ; j < 12 ; j++)
{
/*
*
* Error occurs here
*
*/
brickArray[i,j] = (GameObject) Instantiate(brick, new Vector3(tempBrickStartX, tempBrickStartY, 0), Quaternion.identity);
tempBrickStartX += 1.71f;
}
tempBrickStartX -= 20.52f;
tempBrickStartY -= 0.50f;
}
}
}
I tested out the script you posted and I had no errors, could you post the exact error that unity gives you?
Hello $$anonymous$$arkPDolby,
It's a run-time error. This error is: Invalid Cast Exception: Cannot cast from source to destination type.
This is where it happens: brickArray[i,j] = (GameObject) Instantiate(brick, new Vector3(tempBrickStartX, tempBrickStartY, 0), Quaternion.identity);
Thanks!
I assume the variable brick is assigned to in the editor? Can you double-check that assignment. I could see some oddities if the variable was null in that line, though I'd not have expected a cast error.
You could also try writing the line as
brickArray[i,j] = Instantiate(brick, new Vector3(tempBrickStartX, tempBrickStartY, 0), Quaternion.identity)as GameObject;
But I doubt it'll make much difference.
Also, are there any scripts on the brick prefab/GameObject?
If you used as, you wouldn't get a null reference, you would put null into the array since it will not raise a null exception with as. So yeah, it will make a difference in a potentially bad way.
Answer by Meltdown · Nov 27, 2012 at 05:09 PM
Try changing the line to...
brickArray[i,j] = GameObject.Instantiate(brick, new Vector3(tempBrickStartX, tempBrickStartY, 0), Quaternion.identity) as GameObject;
if the resulting call to Instantiate is null, then null is being shoved into the array, i don't think that is what is desired. no exception will be thrown, the developer could check for null and not insert it, but still that doesn't feel desirable, could be wrong though.
Thanks for your help, but in this case, the problem was inexperience on my part!
Please mark as answered so we know your problem is resolved.