Hey Guys,
maybe its a real easy problem, but i'm a very beginner with Unity :)
I'm building a RTS Game, and actually i have a little map, and a GUI with 2 Icons for 2 seperate buildings. Now when i click one of the GUIs it instatiates the building and yeah, i can build it. But when i click on the other GUI afterwards, it instaites the other building but moves the first instatiation with the actual one. So i'm moving the new building and the old one i just built. Error is: NullReferenceException: Object reference not set to an instance of an object.
The Code of both GUIs:
var building : Transform;
var gridSpacing = 2;
var instant;
var ray;
var hit:RaycastHit;
function OnMouseDown() {
if(buildingCounts.at_Buildt == true)
{
/* var p : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (Input.mousePosition.x, Input.mousePosition.y, 30));
Instantiation = Instantiate(building, p, Quaternion.identity);
*/
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100))
{
Debug.DrawLine (ray.origin, hit.point);
instant = "";
instant = Instantiate(building, Vector3(hit.point.x, 2.5, hit.point.z),Quaternion.identity);
instant.name = "Building2_"+(buildingCounts.BUILDING2+1);
}
buildingCounts.at_Buildt = false;
}
}
function Update() {
if(buildingCounts.at_Buildt == false)
{
//var p : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y - 30, 30));
//Instantiation.transform.position = p;
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100))
{
Debug.DrawLine (ray.origin, hit.point);
instant.position.x = hit.point.x;
instant.position.z = hit.point.z;
instant.position.y = 2.5;
instant.position.x = Mathf.Round(instant.position.x / gridSpacing) * gridSpacing;
instant.position.z = Mathf.Round(instant.position.z / gridSpacing) * gridSpacing;
}
}
}
Would be thankful for every help!