Unity cannot find script of another GameObject

I have a problem getting a variable from a script from another GameObject. I used this type of links before, and I know how it works, but for some reason it says that it cannot find the script I mean.

Code related to another code (if instructions are at the beginning 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; } 

}

An open GameObject 'Player' is defined in Unity as an object that contains the "FirstPersonController" script as a component.

The code means (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); } 

Errors are read: Assets / EnemySight.cs (53.41): Error CS0246: The type or namespace name 'FirstPersonController' could not be found. Do you miss directions for use or build links? And: Assets / EnemySight.cs (59,41): error CS0246: The type or namespace name 'FirstPersonController' could not be found. Do you miss the usage directive or assembly reference?

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

What am I doing wrong? I searched on Google and StackOverflow, but I can not find what the problem is.

Thanks!

+5
source share
1 answer

If there is any namespace declaration in your FirstPersonController class, you need to declare the use of your EnemySight code. As well as:

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

and...

 using MyNamespace.Blah; public class EnemySight : MonoBehaviour { ... } 

for monodevelop, you can use alt + space when declaring classes that are not yet used in your scope, and it will place the class on top of you.

+3
source

Source: https://habr.com/ru/post/1235691/


All Articles