1

I am trying to access the ThirdPersonCharacter script (specifically the isPoweredUp variable) that is attached to my mainPlayer object, from the power-up.

The goal is when the player collides with the power up, for it to flag the isPoweredUp, and then destroy the power up object.

Here is what I have done so far:

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);


        }
    }
}

On my mainPlayer object, I have a public variable called isPoweredUp, currently set to false.

When i try to run this I get the following error:

The type or namespace name ThirdPersonCharacter could not be found

What am I doing wrong here?

EDIT:

This is the ThirdPersonCharacter script attatched to my mainPlayer GameObject:

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;
        ...
    }
}
3
  • "On my mainPlayer object, I have a public variable called isPoweredUp" -- the code you posted seems to contradict that statement. In any case, the error message you quoted usually comes with a second half: "(are you missing a using directive or an assembly reference?)". So...are you? Where is that type defined? Why do you think the code in which you're using it should know about that type? Commented Oct 11, 2015 at 7:44
  • I added the code that I am attempting to reference, attached to my mainPlayer object Commented Oct 11, 2015 at 17:03
  • To be precise (which is important of course!) "On my mainPlayer object, I have a public variable called isPoweredUp" is wrong: from the code it looks like you understand, but to be sure this sentence should be "On my mainPlayer object I have a component ThirdPersonCharacter with a public variable called isPoweredUp". Commented Oct 12, 2015 at 16:36

1 Answer 1

1

The namespace was messing with the attempted reference. By removing it, i was able to get it working.

So i just removed

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

and now its working.

Sign up to request clarification or add additional context in comments.

1 Comment

The only line you need to remove there is the namespace. You're right though, conventionally Unity components don't use namespaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.