Timmeh
Timmeh

Reputation: 139

Unity cannot find script of other GameObject

I have a problem getting a variable from script from another GameObject.. I have used this type of referring before and I know how it works, but for some reason it's saying it can't find the script I am referring to.

The code that is referring to the other code (if statements at the start of CanHearPlayer()):

using UnityEngine;
using System.Collections;

public class EnemySight : MonoBehaviour {

public GameObject Player;
public float fieldOfViewDegrees = 30;
public float visibilityDistance = 50;
public bool SeeingPlayer;
public float deathDistance;
public float hearDistance;

void Update(){
    SeeingPlayer = CanSeePlayer();
    float Distance = Vector3.Distance(transform.position, Player.transform.position);

    if ((SeeingPlayer == true)) {
        transform.LookAt(Player.transform.position);

        if (Distance < deathDistance){
            Debug.Log("You died");
            //Game over sequence starts here
        }
    }

    if (CanHearPlayer () == true) {
        Debug.Log ("I can hear you.");
    }
}

protected bool CanSeePlayer()
{
    RaycastHit hit;
    Vector3 rayDirection = Player.transform.position - transform.position;

    if ((Vector3.Angle(rayDirection, transform.forward)) <= fieldOfViewDegrees * 0.5f)
    {
        // Detect if player is within the field of view
        if (Physics.Raycast(transform.position, rayDirection, out hit, visibilityDistance))
        {
            return (hit.transform.CompareTag("Player"));
        }
    }

    return false;
}

protected bool CanHearPlayer(){

    RaycastHit hit;
    Vector3 rayDirection = Player.transform.position - transform.position;

    if (Player.GetComponent<FirstPersonController>().MakingWalkingSound == true) {
        hearDistance = 50;
    } else {
        hearDistance =  5;
    }

    if (Player.GetComponent<FirstPersonController>().MakingRunningSound == true) {
        hearDistance = 100;
    }

    if (Physics.Raycast(transform.position, rayDirection, out hit, hearDistance))
    {
        return (hit.transform.CompareTag("Player"));
    }
    return false;
}

}

The public GameObject 'Player' is defined in Unity as the object which contains the 'FirstPersonController' script as a component.

Code it is referring to (part of it):

public class FirstPersonController : MonoBehaviour
{
    public bool MakingWalkingSound;
    public bool MakingRunningSound;

private void GetInput(out float speed)
{
     // Read input
    float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    MakingWalkingSound = !(horizontal == 0 && vertical == 0);

    MakingRunningSound = Input.GetKey(KeyCode.LeftShift);
}

The errors read: Assets/EnemySight.cs(53,41): error CS0246: The type or namespace name 'FirstPersonController' could not be found. Are you missing a using directive or an assembly reference? And: Assets/EnemySight.cs(59,41): error CS0246: The type or namespace name 'FirstPersonController' could not be found. Are you missing a using directive or an assembly reference?

These lines correspond to the first two if-statements in CanHearPlayer.

What am I doing wrong? I've searched on Google and StackOverflow, but I cannot find what the problem is..

Thanks!

Upvotes: 6

Views: 3050

Answers (1)

Frohlich
Frohlich

Reputation: 963

If there is some namespace declaration on your FirstPersonController class you need to declare using on your EnemySight code. Just like:

namespace MyNamespace.Blah { 
    public class FirstPersonController : MonoBehaviour {
        ...
    }
}

and...

using MyNamespace.Blah;

public class EnemySight : MonoBehaviour {
    ...
}

for monodevelop you can use alt+spacebar when declaring classes that are not already in your using scope and it will place using on top of the class for you.

Upvotes: 6

Related Questions