- Home /
Placing A Prefab Model With Preview System - Unexpected Error/Bug
Hello! I am trying to work on a similar system to the DayZ Epoch building system where a user can place a wall in game and be able to preview the wall before placing it, however, I have encountered a few problems.
Essentially, what the program is doing is listed below:
If the user clicks, spawn in a prefab of a wall which is colored blue, is transparent and has no collision detection. The prefab wall is then made a child of the main camera to it moves around with the player.
If the user clicks a second time, the program spawns in a prefab of the actual wall which has all the normal things, such as collision detection ETC. The preview wall is then deleted. This part is where a problem occurs.
For some reason, the second paragraph can not seem to access any part of the prefab preview wall. It cannot place the wall in the position or rotation of the preview wall and it also cannot destroy the preview wall.
Below is my code for the entire "BuildWall.js" file, please note that the wall can be placed fine at set (static) or another object's coords. This code compiles fine but responds with the error when the second paragraph attempts to access the preview wall GameObject variable:
NullReferenceException: Object reference not set to an instance of an object
BuildWall.Update () (at Assets/BuildWall.js:38)
I am not particularly sure why this is happening, so I cannot think of any fixes. It would be very helpful if someone could tell me
a) why this is happening
and
b) what I can do to get around it.
I have not been able to do much research on this because I just don't know what to search for :S
Below is the code: #pragma strict
var InvisWall : GameObject;
var Wall : GameObject;
var Placing : boolean = false;
function Start () {
}
function Update () {
if(Input.GetMouseButtonDown(0)){ // If left click down
if(Placing == false){ // If there is not currently a wall be previeewed
// There is now a wall being previewed
Placing = true;
// Define a variable for the preview
var PrePlaceWall : GameObject = Instantiate(InvisWall, transform.position + transform.forward * 10, transform.rotation);
// Make sure that the preview wall is a child of the camera so it moves with the camera
PrePlaceWall.transform.parent = transform;
} else{ // If there is currently a wall being previewed
// There is no longer a wall being previewed
Placing = false;
// Place the real wall in the position of the preview wall
Instantiate(Wall, PrePlaceWall.transform.position, PrePlaceWall.transform.rotation);
// Destroy the Preview wall
Destroy(PrePlaceWall);
}
}
}
Thanks in advance!
~Joe
Your answer
Follow this Question
Related Questions
how to replace multiple game objects with different transform component with same prefab? 1 Answer
Multiple navMeshes? (swapping navMeshs at runtime) 0 Answers
Randomly spawn the same object with different properties consistently. 2 Answers
Is there a difference in adding components to a GameObject? 3 Answers
Add a GameObject as a sub-asset of a ScriptableObject 1 Answer