Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

calling a variable from another method (in another file)

Discussion in 'Scripting' started by argeunes, Jul 24, 2019.

  1. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    This is probably a very "noob" question...but I am attempting to call a variable from another C# file and am having difficulty. I will provide my code..

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8.  
    9. public class UserVitalStats : MonoBehaviour
    10. {
    11.     CharacterController charController;
    12.     FPCharacterController playerController; //THIS IS WHERE I CANT GET IT TO WORK
    13.  
    14.     void Start()
    15.     {
    16.         charController = GetComponent < CharacterController> ();
    17.         playerController = GetComponent<FPCharacterController>(); //ALSO WHERE I CANT GET IT TO WORK
    18.     }
    19.  

    CALLING FROM

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class FPCharacterController : MonoBehaviour
    6. {
    7.  
    8.     public Rigidbody rigidBody;
    9.     public FPCharacterController playerController;
    10.  
    11.  void Start()
    12.     {
    13.         playerController = GetComponent<FPCharacterController>();
    14.     }
    15. }
    16.  
    I had other code involved but just used the necessary to shorten it...if I need to I can put the full code
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,304
    What are you trying to do? Why do you have the FPCharacterController with a variable to an FPCharacterController?

    If UserVitalStats is on the same GameObject as the FPCharacterController, then the GetComponent call will work as you have written.
     
  3. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    Wow now I see where I went wrong haha. Cut and paste issues. Thanks for the extra set of eyes with the FP Character controller situation! I've been looking at code for hours o_O
     
unityunity