I can access the field but it wont update the value?

Hey there. I’m having a rough time with this one.
I have a script that executes a method within its Update and returns a float. This is called angleDegrees, but it’s private, so I made an accessor to be able to read from it externally, like so:

public float angleDisplay
    {
        get
        {
            return angleDegrees;
        }
        set
        {
            angleDegrees = value;
        }

    }

(posting it just in case I might have messed up)
Then I proceed to call it from another script, by making another float field and accessing it on Start. I need it for a constant if check so it needs to be real time.

lookAngle = GameObject.Find("Player").GetComponent<PlayerMouseLook>().angleDisplay;

Aaaand… well, it doesn’t work. I’m not sure where I might be messing up. I checked the field (angleDegrees) in Debug mode and it does update in real time, lookAngle however remains unchanged. I also tried making angleDegrees public and accessing it, in case I was maybe screwing the accessor part, but it didn’t work either.

If you could throw me a bone here I’d really appreciate it.
(oh and I didn’t post the full code because it is a giant ball of mess but if you need it I can do so)
Cheers!

The problem is, you’re accessing angleDisplay in Start(). Start is called only once and before any Update method runs, so this will grab its current value at the start of the script (and never again). If angleDegrees gets updated every frame you’ll have to grab it via accessor every frame as well.

You need to make a reference to the PlayerMouseLook script, then access the angleDisplay when you need lookAngle updated. Also, use FindGameObjectWithTag(“Player”) instead of Find(), it’s less expensive.

public class PlayerMouseLook : MonoBehaviour
{
    [SerializeField] float angleDegrees;

    public float AngleDisplay
    {
        get { return  angleDegrees; }
        set { angleDegrees = value; }
    }
}
public class OtherScript : MonoBehaviour
{
    PlayerMouseLook mouseLook;
    float lookAngle;

    void Awake()
    {
        mouseLook = GameObject.FindGameObjectWithTag( "Player" ).GetComponent<PlayerMouseLook>();
    }

    void Update()
    {
        lookAngle = mouseLook.AngleDisplay;
    }
}

I used PascalCase for my accessor, so note the difference between your file. Most accessors are created this way because they typically carry the same name as the variable they contain. It also allows you to easily differentiate between a regular variable and a property…

Okay, I found that if I set the field from the original script to override lookAngle, it works and updates in real time, but I’d like to be able to access it the other way around since I feel it’s not the most elegant solution coding-wise. Could there be a workaround for this situation?

Somehow I can’t see your comment so I’ll quote it from the e-mail notification lol

@TreyH

By “in real time”, are you editing the value in Update() on another script?

In script 1, I have angleDegrees which is the return of a method that runs in Update().

Script 2 holds lookAngle, which is needed as a read-only to calculate something else, also in real time, but it is assigned in Start() and attempting to fetch it from script 2 doesn’t work.

Now, if I sort of ‘override’ lookAngle from script 1 towards script 2, this works.

Actually I think I might be onto the problem here. I assumed that the ‘updatey’ nature of angleDegrees was inherited automatically inside the other script, and thus lookAngle would update in realtime aswell. This is not the case, right? Assigning it on Start only applies it… on start. But then how should I turn the functionality to work inside of Update()?

Should I just call
lookAngle = GameObject.Find("Player").GetComponent<PlayerMouseLook>().angleDisplay;

in Update()? It seems rather messy…