text: 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!
  Your wording is indicating some confusion in how these things work, so I'd like to clarify your problem.
  Then I proceed to call it from another script, by making another float field and accessing it on Start
  Then I proceed to call it from another script, by making another float field and accessing it on Start
  So are you accessing and storing a reference to the field in the Start() function and trying to manipulate it later? If that's the case, you are going to have problems as Unity's supported versions of .NET do not yet support value references being stored as variables.
  Have you tried storing a reference to your Component script and editing the value of its field?
  Answer by karmaral
  ·
  Dec 27, 2017 at 05:32 PM
  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?
  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 calllookAngle = GameObject.Find("Player").GetComponent<PlayerMouseLook>().angleDisplay;
  lookAngle = GameObject.Find("Player").GetComponent<PlayerMouseLook>().angleDisplay;
  in Update()? It seems rather messy...
  Just store the component reference ahead of time.
  // Keep a reference to the target PlayerMouseLook
  private PlayerMouseLook playerMouse;
  // Use this for initialization
  void Start()
  {
  this.playerMouse = GameObject.Find("Player").GetComponent<PlayerMouseLook>();
  }
  // Update is called once per frame
  void Update()
  {
  Debug.Log(this.playerMouse.angleDisplay);
  }
  Right, I had thought of that earlier on but ended up discarding it because it wasn't working (I was missing the Update part)
  Thank you!
  Great! If this has answered your problem, please convert the comment to an answer and mark it as the solution. That way we can close the thread and mark this problem as solved.
  Answer by karmaral
  ·
  Dec 27, 2017 at 11:37 AM
  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?
  Answer by ransomink
  ·
  Dec 27, 2017 at 11:33 PM
  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...
  Thank you, valuable information indeed, I appreciate it!
  Were you able to fix it now?
  Yes! I had marked it as answered already earlier on, does it not appear like so? The site's been working a bit funky for me.
  I want my script to wait 2 seconds before continue in a condition, in update, using C#
  2
  Answers
  How can i make my spaceship to land automatic on the Base ?
  1
  Answer
  Why are my instantiated projectiles not firing?
  0
  Answers
  Can't Resources.Load ScriptableObject in Static class
  0
  Answers
  Problems with scripting and invokeRepeating
  1
  Answer