- Home /
NullReferanceException layerMask script c# to unityscript.
Hi all,
This has been driving me nuts for the last three days, trying to figure out why i am getting this nullreferanceexception.
I converted this code from c# to unityscript.
C#
// create a new piece from a specific prototype on a specific position with a specific piece type
private GameObject CreateNewPiece(Vector2 piece, Vector2 pos, string pType)
{
GameObject puzzlePiece = null;
// get piece prototype from main
Transform basePiece = main.GetPiece("" + piece.y + "" + piece.x, pType);
if (basePiece != null)
{
// prototype has been found so make an instance
puzzlePiece = GameObject.Instantiate(basePiece.gameObject, new Vector3(pos.x * 2F, pos.y * -2F, 0), Quaternion.Euler(new Vector3(0, 180, 0))) as GameObject;
// add collider to puzzle Pience
puzzlePiece.AddComponent("BoxCollider");
}
// add to specific layer for fast future RayCasting
puzzlePiece.layer = main.layerMask;
return puzzlePiece;
}
And this is what i came up with in UnityScript.
// create a new piece from a specific prototype on a specific position with a specific piece type
private function CreateNewPiece(piece : Vector2, pos : Vector2, pType : String) : GameObject
{
var puzzlePiece : GameObject = null;
// get piece prototype from main
var basePiece : Transform = main.GetPiece("" + piece.y + "" + piece.x, pType);
if (basePiece != null)
{
// prototype has been found so make an instance
puzzlePiece = Instantiate(basePiece.gameObject, Vector3(pos.x * 2F, pos.y * -2F, 0), Quaternion.Euler(Vector3(0, 180, 0)));
// add collider to puzzle Pience
puzzlePiece.AddComponent("BoxCollider");
}
// add to specific layer for fast future RayCasting
puzzlePiece.layer = main.layerMask;
return puzzlePiece;
}
The line of code that is giving me the nullreferenceexception is in this line.
puzzlePiece.layer = main.layerMask;
The code main in main.layerMask refers to a script called JigsawMain and in that script there is a class which contains the layerMask variable.
I think perhaps the two scripts might not be communicating correctly.
Any ideas?
Thanks.
Daniel.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to check for null 2 Answers
Why does my slide out menu not work when i translate from JS to C#? 1 Answer
How to use Android Dual Joysticks? 1 Answer