Question by
Loki1991 · Aug 11, 2017 at 06:48 AM ·
c#gameobject
collider.gameObject.GetComponent doesn't have correct values yet it has the correct name
I have looked around and am having trouble finding an answer. trying to make a match 3 game
I have a multidimensional array called Matrix, this is my grid/board.
i have a normal game object array that is used to link these objects from the inspector.
this.Matrix[i, j].GameObject = this.go[i * Constants.MaxColumns + j];
this.Matrix[i, j].CurrentI = i;
this.Matrix[i, j].CurrentJ = j;
if i call a debug after this to see the value it returns the correct i and j it does however in the next method called by an on click i retrive the object clicked through
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit = Physics2D.Raycast(ray.origin, ray.direction);
var tempPiece = hit.collider.gameObject.GetComponent<Piece>();
however now tempPiece.CurrentI and CurrentJ return 0. However if i get the name value it does have the correct name for the object.
here is the full start method and then the update method only calls the checkpiece method
private void Start()
{
this.gameState = GameState.Start;
var index = Random.Range(0, Constants.MaxSize);
for (var i = 0; i < Constants.MaxColumns; i++)
for (var j = 0; j < Constants.MaxRows; j++)
{
var point = this.GetScreenCoordinatesFromVieport(i, j);
this.go[i * Constants.MaxColumns + j].transform.position = point;
// add the item
this.Matrix[i, j] = this.gameObject.AddComponent<Piece>();
// sets the coordinates of the item in the item for reference later
this.Matrix[i, j].GameObject = this.go[i * Constants.MaxColumns + j];
this.Matrix[i, j].SetIJ(i, j);
// this.Matrix[i, j].CurrentI = i;
// this.Matrix[i, j].CurrentJ = j;
// Debug.Log(i+","+j);
// Debug.Log(this.Matrix[i, j].CurrentI+","+this.Matrix[i,j].CurrentJ);
// Debug.Log(this.Matrix[i, j].CurrentI + "," + this.Matrix[i, j].CurrentJ);
// add a box collider the the raycast to work properly
if (this.Matrix[i, j].GameObject.GetComponent<BoxCollider2D>() == null)
this.Matrix[i, j].GameObject.AddComponent<BoxCollider2D>();
}
}
private void CheckPieceInput()
{
if (Input.GetMouseButtonUp(0))
{
// Debug.Log("clicked");
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit = Physics2D.Raycast(ray.origin, ray.direction);
// check if it hit
if (hit.collider != null)
{
Debug.Log("hit something: " + hit.collider.gameObject.name + " " + hit.collider.gameObject.GetComponent<Piece>().CurrentI + ":" + hit.collider.gameObject.GetComponent<Piece>().CurrentJ);
var tempPiece = hit.collider.gameObject.GetComponent<Piece>();
// Debug.Log(tempPiece.CurrentI + "," + tempPiece.CurrentJ);
if (this.PieceOne == null)
{
this.PieceOne = tempPiece;
}
else
{
this.PieceTwo = tempPiece;
this.Swap(
this.PieceOne.CurrentI,
this.PieceOne.CurrentJ,
this.PieceTwo.CurrentI,
this.PieceTwo.CurrentJ);
// this.AnimateMovement(10f);
this.MoveIt();
// sets first back to null to start selection again
Debug.Log("2nd");
this.PieceOne = null;
this.PieceTwo = null;
}
}
}
}
Comment