NullReferenceException: Object reference not set to an instance of an object LookAt.Update () (at Assets/Scripts/LookAt.js:11)

how would i go about looking at the ‘other’ in OnTriggerEnter??? the two are in different gameobjects so the OnTriggerEnter is on one gameobject, but i want to send the trigger/object that collided to another object that then looks at that target, but for the life of me cant figure it out here is my current set up…

attackRTriggereStay.JS

@HideInInspector 
var targetAquired = false;
var attackTarget; 

function OnTriggerEnter (other : Collider) {
	
	print(other);
	print(targetAquired);
	
	if(other.gameObject.CompareTag ("enemy")){
		
		attackTarget = other;
		targetAquired = true;
		
		print(attackTarget);
		print("targetAquired");
	}
		
}

function OnTriggerExit (other : Collider) {

if(other.gameObject.CompareTag ("enemy")){
		
		targetAquired = false;
		
		print(attackTarget);
		print(targetAquired);
	}

}

LookAt.JS

var getTrigger : attackRTriggerStay;
var getTarget : attackRTriggerStay;

function Start(){

getTrigger = GetComponent(attackRTriggerStay);


}

function Update () {
	
	getTarget =gameObject.GetComponent(attackRTriggerStay);
	
	if(getTrigger.targetAquired == true){
	
		transform.LookAt(getTarget.other);
}

}

Was it you who I told that you can merge getTrigger and getTarget into one variable? If you don't understand this you should read something about OOP! Two variables referring to the exact same component, so you can leave out one.

yeah i think it was, but i was thinking it wasn't working so a thought that crossed my mind was that you cant send an objects transform stuff and a boolean from the same one.... dont know why i thought that, but i have been trying to get this thing to work for a while now.....

2 Answers

2

I think you want to LookAt other.transform

tried that.... didnt work... lol but thank you for the reply

You cannot access to the other variable as it is a local variable of the OnTriggerEnter method.

you should do the following :

  • use a TargetComp var to store the component attackRTriggerStay (it seems that it will not change dynamicly)
  • Reset to null the attackTarget when no target is available
  • Set the gameObject to the attackTarget (not the collider) : attackTarget = other.gameObject
  • You want to lookAt a direction, not a gameObject

Code :

function Start()
{
  TargetComp = gameObject.GetComponent(attackRTriggerStay);
}

function Update(){
  if(TargetComp.attackTarget != null){

    // Use some smoothing here
    transform.rotation = Quaternion.Lerp(
        transform.rotation,
        Quaternion.LookAt(
           TargetComp.attackTarget.transform.position - transform.position
        ), 
        Time.time
    );
  }
}

targetcomp didnt come up in the scripting reference.... what is it exactly?

and when i changed the other to other.GameObject its now sending me a null. when before it was giving me the name of the object? why would that be?

Sorry, I made a mistake on the case : other.gameObject is correct case. TargetComp is not in the scripting reference, it is a local variable that you define to store a reference to your component.

it isnt recognizing the TargetComp at all..... this is so frustrating....its just giving me a null