Waypoint Arrow not pointing at tagged object ?

Hi ,strange problem here as usual lol ,I am using a simple piece of code which is attached to a cone mesh which is a child of the main camera and I want it to point at my tagged waypoints in turn ,so I start simple and use waypoint 1 (tagged as “1”) and the arrow rotates but actually points at the center of my terrain and not the tagged waypoint ?
(I have checked and the terrain is not tagged at all) ? heres the JS (pragma strict) .
Any clue ?

#pragma strict
function Update () {
     
    var object = GameObject.FindGameObjectWithTag("1");
    var target = object.transform;
    transform.LookAt(target , Vector3(0,0,1));
    transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);

}

lol fixed ,I had a DUH moment after I posted ,the Original code was wrong the line

transform.LookAt(target , Vector3(0,0,1));

should be

transform.LookAt(target);

Hope this helps people with arrow pointer problems lol :slight_smile:

1 Answer

1

FindObjectOfType() is very slow and should not be done every frame. Typically you would give the game object with the script attached a name know name and use GameObject.Find() to get the object in start (or drag and drop into a public variable). I also don’t see where ckp is declared or set in this code, so I cannot check how that is being used. And you execute code even if object has not been initialized. Here is your code with a couple of mods:

#pragma strict

var other : CheckPoint01;

function Start () {
 	other = GameObject.Find("Other_Name").GetComponent(CheckPoint01);
}

function Update () {
    var object : GameObject = null;
    
    if (other.ckp==1) 
    	object = GameObject.FindGameObjectWithTag("1");
    if (other.ckp==2) 
    	object = GameObject.FindGameObjectWithTag("2");
 	
 	if (object != null) {
    	var target = object.transform;
 
		transform.LookAt(target);
 		transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
 		}
}

I assume the transform.rotation is because the natural rotation of the arrow is not towards positive ‘z’?

var ckp =0; this is setup in CheckPoint01.js ps what do I put in other_name field ,sorry im a bit lost still lol I get this error ? Assets/direx.js(3,13): BCE0018: The name 'CheckPoint01' does not denote a valid type ('not found'). Is it because the checkpoint script is not on the same object ? and can I get a value from a script on a different object ?

–

I edited the code above to access ckp in other. "Other_Name" will be the name of the game object that the CheckPoint01 script is attached to. As for the error, the type and the file name must match exactly. That is "Checkpoint01.js" is not "CheckPoint01.js" If the names match, then the error should go away.

–

As I said above, the names must match exactly. So you can either change the name of the script to "CheckPoint01", or you can change the type used above to "Checkpoint01". Even a capital letter difference is enough to generate the error.

–

Thanks lol silly mistake :p

–

But you don't say what is happening. Are you getting a compile time error, a runtime error, or is the value in the second script not what you want?

–