从另一个对象访问变量

时间:2015-10-11 06:59:49

标签: c# variables unity3d reference

我正在尝试从启动时访问附加到我的mainPlayer对象的ThirdPersonCharacter脚本(特别是isPoweredUp变量)。

目标是当玩家与电源发生碰撞时,它会标记isPoweredUp,然后销毁电源对象。

这是我到目前为止所做的:

using UnityEngine;
using System.Collections;

public class pickUp: MonoBehaviour
{  

    public GameObject mainPlayer;
    public GameObject pickupPrize;
    public GameObject playerScript;

    private ThirdPersonCharacter thirdPersonCharacter;

    void Awake() {
        mainPlayer = GameObject.FindGameObjectWithTag("Player");
    }

    void Start()
    {

        pickupPrize = GameObject.FindGameObjectWithTag("Pickup");
        thirdPersonCharacter = mainPlayer.GetComponent<ThirdPersonCharacter>();

    }

    void  OnTriggerStay(Collider thePlayer){

        {
            thirdPersonCharacter.isPoweredUp = true;
            Destroy(pickupPrize);


        }
    }
}

在我的mainPlayer对象上,我有一个名为isPoweredUp的公共变量,目前设置为false

当我尝试运行时,我收到以下错误:

The type or namespace name ThirdPersonCharacter could not be found

我在这里做错了什么?

编辑:

这是附加到我的mainPlayer GameObject的ThirdPersonCharacter脚本:

Using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof(Rigidbody))]
    [RequireComponent(typeof(CapsuleCollider))]
    [RequireComponent(typeof(Animator))]

    public class ThirdPersonCharacter : MonoBehaviour
    {
        [SerializeField] float m_MovingTurnSpeed = 360;
        [SerializeField] float m_StationaryTurnSpeed = 180;
        [SerializeField] float m_JumpPower = 12f;
        [Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f;
        [SerializeField] float m_RunCycleLegOffset = 0.2f; 6
        [SerializeField] float m_MoveSpeedMultiplier = 1f;
        [SerializeField] float m_AnimSpeedMultiplier = 1f;
        [SerializeField] float m_GroundCheckDistance = 0.1f;

        Rigidbody m_Rigidbody;
        Animator m_Animator;
        bool m_IsGrounded;
        float m_OrigGroundCheckDistance;
        const float k_Half = 0.5f;
        float m_TurnAmount;
        float m_ForwardAmount;
        Vector3 m_GroundNormal;
        float m_CapsuleHeight;
        Vector3 m_CapsuleCenter;
        CapsuleCollider m_Capsule;
        bool m_Crouching;
        public bool isPoweredUp;
        ...
    }
}

1 个答案:

答案 0 :(得分:1)

命名空间搞乱了尝试的引用。通过删除它,我能够使它工作。

所以我刚刚删除了

 namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Animator))]

现在正在工作。