其他脚本中的静态变量“在当前上下文中不存在”

问题描述 投票:0回答:0

我写了一个脚本,这样当我按下一个按钮时,它会将变量

isSentToSpawn
设置为 1。我将它声明为
public static int
并试图从用于移动播放器的脚本访问它。它正在抛出错误
The name 'PlayerInteract' does not exist in the current context.

PlayerInteract.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInteract : MonoBehaviour
{
    public float Distance;
    public GameObject keyDisplay;
    public GameObject actionText;
    public AudioSource buttonClickSoundEffect;
    public GameObject player;
    public static int isSentToSpawn;

    void Update()
    {
        Distance = PlayerCasting.distanceFromTarget;
    }

    void OnMouseOver() {
        if (Distance <= 50) {
            keyDisplay.SetActive(true);
            actionText.SetActive(true);
            Debug.Log ("hover");
            if (Input.GetButtonDown("Interact")) {
                isSentToSpawn = 1;
                buttonClickSoundEffect.Play();
            }
        } else if (Distance >= 60) {
            keyDisplay.SetActive(false);
            actionText.SetActive(false);
        }
    }
    void OnMouseExit() {
            keyDisplay.SetActive(false);
            actionText.SetActive(false);
    }
}

FirstPersonController.cs(给出错误的部分)

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;

namespace UnityStandardAssets.Characters.FirstPerson
{
    [RequireComponent(typeof (CharacterController))]
    [RequireComponent(typeof (AudioSource))]
    public class FirstPersonController : MonoBehaviour
    {
private int PlayerSentToSpawn;
        
        private void sendToSpawn() {
            PlayerSentToSpawn = PlayerInteract.isSentToSpawn;
            if ( PlayerSentToSpawn == 1 ) {
                m_CharacterController.transform.position = new Vector3 (0, 0, 0);
            }
        }
    }
}

对 unity 很陌生,所以感谢解释

c# unity3d static-variables
© www.soinside.com 2019 - 2024. All rights reserved.