2015-10-11 248 views
1

我試圖從啓動時訪問附加到我的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 

我在做什麼錯在這裏?

編輯:

這是attatched我mainPlayer遊戲對象的腳本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; 
     ... 
    } 
} 
+0

_「在我的mainPlayer對象上,我有一個名爲isPoweredUp的公共變量」 - 您發佈的代碼似乎與該聲明相矛盾。在任何情況下,您引用的錯誤消息通常會帶有後半部分:_「(您是否缺少使用指令或程序集引用?)」_。你也是?該類型在哪裏定義?你爲什麼認爲你使用它的代碼應該知道這種類型? –

+0

我添加了我嘗試引用的代碼,附加到我的mainPlayer對象 –

+0

準確地說(當然這很重要!)*「在我的mainPlayer對象上,我有一個名爲'isPoweredUp'的公共變量」* is錯:從代碼中看起來像你理解的,但要確保這句話應該是*「在我的mainPlayer'對象上,我有一個名爲」ThirdPersonCharacter「的組件,名爲isPoweredUp」*。 – 31eee384

回答

1

的命名空間是與試圖搞亂參考。通過刪除它,我能夠得到它的工作。

所以我只是刪除

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

,現在它的工作。

+1

您需要刪除的唯一行是'namespace'。不過,通常情況下,Unity組件不使用名稱空間。 – 31eee384