Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ducsuus · May 17, 2014 at 10:49 PM · prefabwalls

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges