- Home /
Question by
Toomuchbob · Jan 12, 2013 at 05:04 AM ·
arraytag
Implicit Downcast on array object and clearing array
#pragma strict
var selectedUnit : Transform;
var tileSelected : Transform;
var getSpaces = new Array();
function Start()
{
}
function Update()
{
GetMovementTiles();
if(Input.GetMouseButtonDown(0))
{
print(getSpaces);
if(selectedUnit != null)
{
for(var i = 0; i <= getSpaces.length; i++)
{
var clearColor : Transform = getSpaces[i];
clearColor.renderer.material.color = Color.green;
}
selectedUnit = null;
}
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, 1000) && hit.transform.tag == "Player 1")
{
selectedUnit = hit.transform;
print(selectedUnit);
}
print(tileSelected);
/*if(Physics.Raycast(ray, hit, 1000) && hit.transform.tag == "Terrain" && selectedUnit != null)
{
tileSelected = hit.transform;
print(tileSelected);
}*/
}
}
function GetMovementTiles()
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
while(selectedUnit != null)
{
if(Physics.Raycast(ray, hit, 1000))
{
var coordTest : Transform = hit.transform;
for(var i = 0; i < getSpaces.length; i++)
if(getSpaces[i] == coordTest)
{
//for(var j = getSpaces.length; j >= getSpaces.
return;
}
getSpaces.Add(coordTest);
coordTest.renderer.material.color = Color.red;
return;
}
return;
}
}
So in this code I am trying to do two things that aren't working, #1, take an index of the array, change the material color of the object at that instance of the array to green in the first for statement. I'm getting a Warning about a downcast from object to transform @ line 22 when I leave it as it is
In the GetMovementTiles function at the bottom, I can't seem to set it so it only adds Transforms that are tagged with "Terrain", without freezing unity or breaking the function. Where would I add this to make it work?
Comment