- Home /
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
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);
...
}
}
}
}
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
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.
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
Also that script gets an error: Assets/Random.js(14,44): BCE0005: $$anonymous$$ identifier: 'Block'.
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.
Your answer
Follow this Question
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