- Home /
Cannot attach game object to script.
I made a script for opening a gate but when i try to game attach game object to it , it was not going not even by using small dot besides it . It only shows some fbx objects. Please help
Here is the script
using UnityEngine.UI;
public class DoorCellOpen : MonoBehaviour {
public float TheDistance;
public GameObject ActionDisplay;
public GameObject ActionText;
public GameObject TheDoor;
public AudioSource CreakSound;
void Update () {
TheDistance = PlayerCasting.DistanceFromTarget;
}
void OnMouseOver () {
if (TheDistance <= 2) {
ActionDisplay.SetActive (true);
ActionText.SetActive (true);
}
if (Input.GetButtonDown("Action")) {
if (TheDistance <= 2) {
this.GetComponent<BoxCollider>().enabled = false;
ActionDisplay.SetActive(false);
ActionText.SetActive(false);
TheDoor.GetComponent<Animation> ().Play ("FirstDoorOpenAnim");
CreakSound.Play ();
}
}
}
void OnMouseExit() {
ActionDisplay.SetActive (false);
ActionText.SetActive (false);
}
}
There was no compile errors .
Answer by swanne · Nov 06, 2020 at 03:10 PM
Hi,
Is this the entire script or just a snippet? There is no visible reference to PlayerCasting. I have no idea what that is.
Also, are you trying to attach this code to a game object or trying to assign a game object to one of the 3 game object variables?
Here is the full script with ray casting . The upper common part is same .
public class PlayerCasting : $$anonymous$$onoBehaviour {
public static float DistanceFromTarget;
public float ToTarget;
void Update () {
RaycastHit Hit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out Hit)) {
ToTarget = Hit.distance;
DistanceFromTarget = ToTarget;
}
}
}
//========================
using UnityEngine.UI;
public class DoorCellOpen : $$anonymous$$onoBehaviour {
public float TheDistance;
public GameObject ActionDisplay;
public GameObject ActionText;
public GameObject TheDoor;
public AudioSource CreakSound;
void Update () {
TheDistance = PlayerCasting.DistanceFromTarget;
}
void On$$anonymous$$ouseOver () {
if (TheDistance <= 2) {
ActionDisplay.SetActive (true);
ActionText.SetActive (true);
}
if (Input.GetButtonDown("Action")) {
if (TheDistance <= 2) {
this.GetComponent<BoxCollider>().enabled = false;
ActionDisplay.SetActive(false);
ActionText.SetActive(false);
TheDoor.GetComponent<Animation> ().Play ("FirstDoorOpenAnim");
CreakSound.Play ();
}
}
}
void On$$anonymous$$ouseExit() {
ActionDisplay.SetActive (false);
ActionText.SetActive (false);
}
}
Your answer
Follow this Question
Related Questions
How can i check/wait until the gameobject will end the rotation ? 1 Answer
How can I add a speed factor to the script ? 0 Answers
How can i check and fire an event when the user look at specific object ? 0 Answers
Sorting a list based of a variable 1 Answer
How can i List objects by name but also in small text or big text or any kind ? 1 Answer