• 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 /
avatar image
0
Question by gulpy3 · Jun 03, 2014 at 03:42 AM · javascriptinstantiate prefab

NullReferenceException: Object reference... when trying to instantiate an object

I'm trying to get an object to instantiate upon click. When I try to test out my script i get

NullReferenceException: Object reference not set to an instance of an object

blockSpawn.Update () (at Assets/scripts/blockSpawn.js:10) as an error. I've been trying to fix this for around 5 hours now with no luck.. I'm new to this so I may be missing something simple. I appreciate any help with this issue. var block : GameObject;
function Start(){ }
function Update () {
var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = Camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 200)) {
} if ( Input.GetMouseButtonDown(0) ) { var createBlock = Instantiate(block, hit.point, Quaternion.identity);
} else {}

} Thanks again for the help guys!
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NoseKills · Jun 03, 2014 at 05:46 AM

The hit in your code can be null. The if (Physics.Raycast (ray, hit, 200)) check is there so that you'd only do stuff if the raycast hits something, but you have left the of clause empty and try to do stuff upon mouse click even when the ray doesn't hit anything.

If you only want to do stuff when the mouse is clicked, then that should be your first condition.

     if ( Input.GetMouseButtonDown(0) )
        {
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 200)) 
         {
             var createBlock = Instantiate(block, hit.point, Quaternion.identity);  
         }
        }

P.S. If you just want to create an object on the screen where the player clicked/touched, you don't need to raycast. Your code checks whether the click hits another object (with a collider) on the screen and tries to create a new object where the click hits the pre-existing object.

Comment
Add comment · Show 2 · Share
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
avatar image gulpy3 · Jun 03, 2014 at 01:11 PM 0
Share

Thanks for the help man. Got it working! I was wondering if you would be willing to help with one more problem. I want it to spawn a block depending on the value of a random number. So far when I try the code below.. it only spawns block 1 even though it is generating numbers up to 20.

     var block1 : Rigidbody2D;
     var block2 : Rigidbody2D;
     
     function Update()
     {
     
     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
     var rand = Random.Range(1.0, 20.0);
     }
     if (Input.Get$$anonymous$$ouseButtonDown(0 && (rand < 10.0)))
     {
         var clone = Instantiate(block1, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection(Vector3(0, 0, 0));
         Debug.Log(rand);
     }
     else if (Input.Get$$anonymous$$ouseButtonDown(0 && (rand > 10.0)))
      {
     var clone2 = Instantiate(block2, transform.position, transform.rotation);
         clone2.velocity = transform.TransformDirection(Vector3(0, 0, 0));
      Debug.Log(rand);
     }
 
     }
avatar image gulpy3 · Jun 03, 2014 at 01:16 PM 0
Share

Ignore the question above, I fixed it! Thanks anyway!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

JS: How to instantiate a prefab from a public class? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

"Object reference not set to an instance of an object" This should work but it doesn't 1 Answer

photocamera flash 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