- Home /
Instantiate 2 different Buildings in RTS doesn't work // Null Reference Exception
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!
Answer by Vidar · Nov 22, 2010 at 07:49 PM
ok problem solved, stupid mistake
At least explain the problem, and how its was solved before accepting it as an answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iTween can't access instantiated child. 1 Answer
Using iTween with a prefab tree. 0 Answers
Can't reference gameObject in NetworkBehaviour.Awake() 2 Answers
Instantiated prefab references point to first prefab instantiated 3 Answers
Why Is the game object not selected? I'd like to understand why. 2 Answers