Basically I have a function called “RandomPosition” which randomly positions the parent of an object at a different place of the map. However, the function will only run when called from inside the if statement here. A NullReferenceException error is caused when it is called from anywhere else. Can anyone figure out why the function can’t be called from anywhere else? (basically I would like it to be called once at the beginning, and once in the if statement.
var score : int;
var specificCollider : GameObject;
var newPosition : Vector2;
RandomPosition();
function OnTriggerExit (other : Collider) {
if(other.gameObject == specificCollider) {
score += 1;
print(score);
RandomPosition();
}
}
function RandomPosition() {
transform.parent.transform.rotation.y = Random.rotation.y;
newPosition = Random.insideUnitCircle * 750;
transform.parent.transform.position.x = newPosition.x;
transform.parent.transform.position.z = newPosition.y;
}
post the script that the error is coming from.
Hi. Do you mean the actual .js file? The script in plain text is already posted on the original post(I accidentally copied it twice in a row accidentally but I’ve since deleted the second copy.)
I’m trying to find out where else you are calling it from. You say it works in the script you provided. So what is the code that’s causing the error?
Lol! I’m getting a Null Reference Error from your post!
Haha forgive my poor explanatory skills. I swear Unity is slowly damaging my brain. I’ve added some comments to help explain. I hope it helps.
var score : int;
var specificCollider : GameObject;
var newPosition : Vector2;
//The next line will give a NullException error. However, when removed. The entire script works.
RandomPosition();
function OnTriggerExit (other : Collider) {
if(other.gameObject == specificCollider) {
score += 1;
print(score);
RandomPosition();
}
//I can't call the RandomPosition function from this line either. It actually has to be INSIDE the if function above or it will not work.
}
function RandomPosition() {
transform.parent.transform.rotation.y = Random.rotation.y;
newPosition = Random.insideUnitCircle * 750;
transform.parent.transform.position.x = newPosition.x;
transform.parent.transform.position.z = newPosition.y;
}
Oh, I see what’s wrong…
If you want that function called just once when the script first initializes, you need to put it inside of Start or Awake like this:
function Start(){
RandomPosition();
}
I can’t remember the difference between Start and Awake, so that’s another conversation, but try that.
Otherwise, you need to put it inside of your Update() function.
That was my first theory too. Unfortunately I get the same error. Very strange behaviour I will be interested to find the reason behind it. Thanks for your help anyway.
Does your game object start off with a parent? or is it parented by another script during OnCollisionEnter() or something? Perhaps the parent is the null?
oh god i’m stupid. I attached the script to another object by accident a while back… sorry for wasting your time!
That is very normal little mistake. I’m still finding that I’m doing things like this:
if(a = b)...
instead of
if (a == b)...
