Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

referencing one script in another

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

  1. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    I know its probably an easy fix, but I am having trouble referencing variables from script "FirstPersonController" in script "UserVitalStats"
    This is what I have:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class UserVitalStats : MonoBehaviour
    9. {
    10.  
    11.     CharacterController charController; //works
    12.     FirstPersonController playerController; //doesn't work--error
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         userHealthSlider.maxValue = userMaxHealth;
    18.         userHealthSlider.value = userMaxHealth;
    19.         userHealthRegen = 0.5f; //may change later?
    20.         userEnduranceSlider.maxValue = userMaxEndurance;
    21.         userEnduranceSlider.value = userMaxEndurance;
    22.         userEnduranceSlider.maxValue = userMaxEndurance;
    23.         userEnduranceSlider.value = userMaxEndurance;
    24.         userEnduranceDegen = 1;
    25.         userEnduranceRegen = 1;
    26.         userCurrentHunger = userMaxHunger;
    27.         userCurrentHydration = userMaxHydration;
    28.         userCurrentSleep = userMaxSleep;
    29.         userCurrentHygiene = userMaxHygiene;
    30.         userCurrentTemp = userNormalTemp;
    31.  
    32.  
    33.         charController = GetComponent<CharacterController>();
    34.         playerController = GetComponent<FirstPersonController>(); //doesn't work--error
    35.     }
    This ^^ was where I am trying to call in the script.

    Below is the script variables I am trying to reference:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class FirstPersonController : MonoBehaviour
    6. {
    7.     //player movement
    8.     [SerializeField] public float moveSpeed = 4.0f;
    9.     [SerializeField] public float runSpeed = 8.0f;
    10.     [SerializeField] public float runSpeedNull;
    11.     [SerializeField] public float rotateSpeed = 100.0f;
    12.     [SerializeField] public float jumpForce = 500f;
    13.     [SerializeField] public float gravity;
    14.     public bool canMoveSideways = true;
    15.  
    16.     public bool isWalking;
    17.     public bool isRunning;
    18. //the rest is irrelevant
    19. }
    I followed a tutorial to the T (I checked multiple times) and am wondering if the specific tutorial led me in the wrong direction. Other tutorials wouldn't match well with what I was trying to accomplish.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,281
    What is the error message?
     
  3. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    264
    It depends on how you have your game object setup. Is FirstPersonController on the same object as UserVitalStats, or is one on a child object of the other? GetComponent only gets components that are on the same object. You may need one of the related methods GetComponentInParent or GetComponentInChildren.
     
  4. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    The error that I am getting is:
    Severity Code Description Project File Line Suppression State
    Error CS0246 The type or namespace name 'FirstPersonController' could not be found (are you missing a using directive or an assembly reference?) Assembly-CSharp C:\Users\Alyssa\Desktop\Cryptic\Unity\Cryptic from Tutorials (maybe use later)\Assets\Scripts\UserVitalStats.cs 115 Active"

    Also, they are on the same game object: (image attached) Capture.PNG
     
  5. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    264
    It may be cliche, but have you tried restarting Unity and VS? If so, the next thing I'd check is where those two scripts are located in your project. Is one accidentally in an editor folder? Are there assembly definition files that need to be configured?
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,281
    Well, error message clearly states the problem. Assembly references are managed by Unity, but using directives you should add yourself.
     
  7. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    They are both in my "Scripts" folder.

    I am fairly new at this, so what using statement should I be inserting then?
     
  8. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    For instance--I tried using namespace.FirstPersonController
    but received the error that it is a namespace but is used like a type
     
  9. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    264
    Nah, doesn't look like their FPC is in a namespace.
    The next thing you can try is to right click on each script in the project folder and select Reimport. If that still doesn't work, you can close Unity, open a file browser and navigate to your project, and then delete the Library folder (should be next to the Assets folder, not inside it). That is safe to do and will cause Unity to rebuild its references. That will cause the first time reopening the project to take a while.
     
  10. argeunes

    argeunes

    Joined:
    Feb 5, 2019
    Posts:
    20
    YES!!!!!!!!! Thank you so much! I've been going over the same code and researching this for the past two days and I just could not find where I went wrong--and it was as simple as reimporting. I won't forget this now. Thanks again!