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 /
avatar image
0
Question by Jackop222 · Apr 14, 2013 at 11:14 AM · errorobject

NullReferenceException: Object reference not set to an instance of an object Random.OnGUI () (at Assets/Random.js:25)

 var Block : Transform;
 var textureIndicator : GUITexture;
 //private var textures = new Array() ;
 //private var selectedTexture : int;
 
 function start(){
 //textures = Resources.Load.A11("Textures", Texture);
 }
 
 function update () {
 
 
 
 
 
 
 }
 function OnGUI(){
 if (GUI.Button (Rect (20,40,80,20), "Start")){
 var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 var hit: RaycastHit;
 
 for(counter=1;counter<=10;counter++){
 var newBlock : Transform = Instantiate (Block, hit.collider.transform.position+hit.normal.normalized, Quaternion.identity);
 //newBlock.renderer.material.mainTexture = textures [selectedTexture];
 newBlock.tag = "Block";
 
 }
     
 
 }
 }


I have that error i dont know what it means, if anyone could help it would make me happy :D

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
1

Answer by Chronos-L · Apr 14, 2013 at 11:22 AM

 function OnGUI(){
    if (GUI.Button (Rect (20,40,80,20), "Start")){
       var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       var hit: RaycastHit; //this is null
  
       for(counter=1;counter<=10;counter++){
          //hit is still null when you try to use it
          var newBlock : Transform = Instantiate (Block, hit.collider.transform.position+hit.normal.normalized, Quaternion.identity);
          ...
       }
    }
 }

The way you declare the ray and hit means that you need to know what object you are clicking on; however, when you put it in a GUI.Button(), it limits where you will be clicking on (the Rect containing the button). Your logic doesn't make sense to me, however this is just a opinion from me, you know the best what you are doing.

To fix the null-ref-exception, do a Physics.Raycast() before you use the hit.

 function OnGUI(){
    if (GUI.Button (Rect (20,40,80,20), "Start")){
       var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       var hit: RaycastHit;
 
       if( Physics.Raycast( ray, hit ) ) {
          for(counter=1;counter<=10;counter++){
             var newBlock : Transform = Instantiate (Block, hit.collider.transform.position+hit.normal.normalized, Quaternion.identity);
             ...
          }
       }
    }
 }

 
Comment
Add comment · Show 9 · 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 Jackop222 · Apr 14, 2013 at 11:36 AM 0
Share

What i was trying to do was make the script place 10 cubes in a random way, but i am new to java so i don't really know what i am doing :D If you could tell me how to do this i would really appreciate it :D

avatar image Chronos-L · Apr 14, 2013 at 11:44 AM 0
Share

A possible way to make 10 cubes in random position

 function OnGUI(){
    if (GUI.Button (Rect (20,40,80,20), "$$anonymous$$ake Cube")){
       for( var i : int = 0; i < 10; ++i ) {
          $$anonymous$$akeCubeAt( Vector3(
             Random.Range( -10.0, 10.0 ),
             Random.Range( -10.0, 10.0 ),
             Random.Range( -10.0, 10.0 )
          )); 
       }     
    }
 }
 
 function $$anonymous$$akeCubeAt( position : Vector3 ) {
    var newBlock : Transform = Instantiate( Block, position, Quaternion.identity ) as Transform;
 }

For you own benefit, do not copy from ready-made script straight away; figure out the logic first before using other's code, or you will have a difficult understanding why things did not work out the way you want it to.

avatar image Jackop222 · Apr 14, 2013 at 11:51 AM 0
Share

Can you explain how this works then because i am new to java i can right some basic things but i don't quite understand this. :D

avatar image Jackop222 · Apr 14, 2013 at 11:54 AM 0
Share

Also that script gets an error: Assets/Random.js(14,44): BCE0005: $$anonymous$$ identifier: 'Block'.

avatar image Chronos-L · Apr 14, 2013 at 12:03 PM 0
Share

I will try my best to break it down to you; please note that if I have skipped anything, it is mainly because I have done this for quite a while, so some of these codes seems self-explanatory to me.

 function OnGUI(){
    //GUI.Button will return true when the button is clicked
    if (GUI.Button (Rect (20,40,80,20), "$$anonymous$$ake Cube")){
       //Loop for 10 times
       for( var i : int = 0; i < 10; ++i ) {
          /*
             Call the function $$anonymous$$akeCubeAt
 
             $$anonymous$$akeCubeAt needs a Vector3 as parameter,
             we provide a Vector3 with random values in 
             the x, y, and z axis.
          */
          $$anonymous$$akeCubeAt( Vector3(
             Random.Range( -10.0, 10.0 ),
             Random.Range( -10.0, 10.0 ),
             Random.Range( -10.0, 10.0 )
          )); 
       }     
    }
 }
 
 /*
   Create a block at a specific position, that specific position is provided as the parameter
 */
 function $$anonymous$$akeCubeAt( certainPosition : Vector3 ) {
    var newBlock : Transform = Instantiate( Block, certainPosition, Quaternion.identity ) as Transform;
 }



You can search the Unity documentations on:

  • GUI.Button

  • Vector3

  • Random.Range

  • Instantiate

to understand how to work with them.


By the way, you are writing in UnityScript, a variation of Javascript; and both of them have nothing to do with Java, I think Javascript is called Javascript to hitchhike on the fame of Java on that time (that what I heard, it might be wrong). Not knowing what language you are using will make you looks bad, and it will make some people around here a bit grouchy.

Show more comments

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

12 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

Related Questions

DontDestroyOnLoad with object on another scene 2 Answers

Object reference not set to... 1 Answer

Having problems destroying an Object 2 Answers

Some Copies of Prefab Detect Clicks But Others Don't 1 Answer

An object reference is required to access non-static member `UnityEngine.Component.transform' 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