Am I getting bracket errors?

I have a code that is supposed to act as a compass display in my game the displays either a north, south, east, or west GameObject when the camera is facing a certain way. Here’s the script…

var player : GameObject;
var north : GameObject;
var south : GameObject;
var east : GameObject;
var west : GameObject;

function Update () {

if(player.transform.rotation <= 45 && player.transform.rotation => -45)
    {
    north.SetActive (true);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.rotation <= -45 && player.transform.rotation => -135)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (true);
    }

if(player.transform.rotation <= -135 && player.transform.rotation => 135)
    {
    north.SetActive (false);
    south.SetActive (true);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.rotation <= 135 && player.transform.rotation => 45)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (true);
    west.SetActive (false);
    }
}

The errors I get look like bracket errors, but I’ve just been learning rotation functions, and have been corrected before. I would show the errors, but there are quite a lot. Help if you can, and if you’d like to see the errors, I’ll put them up.

What exact error are you getting?

Edit: Your => should be >=. This will make lots of errors go away. => is for declaration of lambda functions. >= is for greater than or equal to comparisons.

Transform.rotation is a 4D quaternion, which doesn’t use degrees, and has XYZW elements. It’s not a single number, but should not be used unless you understand quaternions anyway. Theoretically you could use Transform.eulerAngles, but since there’s more than one correct way to represent a given 3D rotation, reading a single element is not advisable since the results are unpredictable. In general, track rotation yourself, and treat transform rotation stuff as write-only.

–Eric

1 Like

I changed a few things, and I got this error…

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
Compass.Update () (at Assets/Compass.js:17)

Here’s the revised script, I can actually play the game, but the code just doesn’t work…

var player : GameObject;
var north : GameObject;
var south : GameObject;
var east : GameObject;
var west : GameObject;

function Update () {

if(player.transform.rotation.y <= 45 && player.transform.rotation.y >= -45)
    {
    north.SetActive (true);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.rotaion.y <= -45 && player.transform.rotation.y >= -135)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (true);
    }

if(player.transform.rotation.y <= -135 && player.transform.rotation.y >= 135)
    {
    north.SetActive (false);
    south.SetActive (true);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.rotation.y <= 135 && player.transform.rotation.y >= 45)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (true);
    west.SetActive (false);
    }
}

Please read what I said about Transform.rotation.

–Eric

they errors are because you’ve typo’d “rotaion” on line 17, but what Eric is explaining is still going to be a problem

consider

//...

if(Vector3.Dot(transform.forward, Vector3.forward) > 0.8f)
{
//... pointing in the same direction as Vector3.forward (to some tolerance, change the number for that)

off the top of my head I can’t remember which of the Vector3.forward/right/-forward/-right would be “north/south/east/west” but you get the idea.

I researched what you were talking about on the docs, and I’m still confused, and I know I did it wrong. Here’s the new script…

var player : GameObject;
var north : GameObject;
var south : GameObject;
var east : GameObject;
var west : GameObject;
var yRotation : float = 5.0;

function Update () {

yRotation += Input.GetAxis("Horizontal");

if(player.Transform.eulerAngles.y <= 45 && player.Transform.eulerAngles.y >= -45)
    {
    north.SetActive (true);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.Transform.eulerAngles.y <= -45 && player.transform.eularAngles.y >= -135)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (true);
    }

if(player.Transform.eulerAngles.y <= -135 && player.Transform.eulerAngles.y >= 135)
    {
    north.SetActive (false);
    south.SetActive (true);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.Transform.eulerAngles.y <= 135 && player.Transform.eulerAngles.y >= 45)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (true);
    west.SetActive (false);
    }
}

I get this error repeatedly when I play my game…

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
Compass.Update () (at Assets/Compass.js:12)

How can I fix these issues?

You’re using “Transform” (i.e. the class) when you should be using “transform” (i.e. the reference to the current gameobject’s transform) on 12, 20 and 28… i’m not sure whether to call 36 “half right” or “all wrong” given that you’ve used both there… :eyes:

Okay, thanks. I tried it and It sort of works. East appears when I look in that direction, and North appears when I look in that direction, but when I look South or West, it displays North. I know it has to have something to do with how my < and > are set up, but I’m not sure what to change

var player : GameObject;
var north : GameObject;
var south : GameObject;
var east : GameObject;
var west : GameObject;
var yRotation : float = 5.0;

function Update () {

yRotation += Input.GetAxis("Horizontal");

if(player.transform.eulerAngles.y <= 45 && player.transform.eulerAngles.y > -45)
    {
    north.SetActive (true);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.eulerAngles.y <= -45 && player.transform.eularAngles.y > -135)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (false);
    west.SetActive (true);
    }

if(player.transform.eulerAngles.y <= -135 && player.transform.eulerAngles.y > 135)
    {
    north.SetActive (false);
    south.SetActive (true);
    east.SetActive (false);
    west.SetActive (false);
    }

if(player.transform.eulerAngles.y <= 135 && player.transform.eulerAngles.y > 45)
    {
    north.SetActive (false);
    south.SetActive (false);
    east.SetActive (true);
    west.SetActive (false);
    }
}