- Home /
please,help me with script
please, help me it give me error " Assets/script/interact.cs(43,43): error CS1061: Type Door[]' does not contain a definition for
ChangeDoorState' and no extension method ChangeDoorState' of type
Door[]' could be found. Are you missing an assembly reference?"with this line of code hit.collider.GetComponents().ChangeDoorState (); and I make it puplic void in the "door" script , please help me
Answer by eldsoki · Jun 18, 2017 at 09:04 PM
this is my full script using UnityEngine; using System.Collections.Generic; using UnityEngine.UI ;
public class interact : MonoBehaviour { public string interactbutton;
public float interactdistance = 3f;
public LayerMask interactlayer; // filter
public Image interacticon; // picture to show interaction
public bool isinteracting;
// Use this for initialization
void Start () {
//set interact icon to be invisible
if (interacticon != null)
{
interacticon.enabled = false;
}
}
// Update is called once per frame
void Update ()
{
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
//shot a ray
if (Physics.Raycast (ray, out hit, interactdistance, interactlayer))
{
//check if we are not interacting
if (isinteracting == false)
{
interacticon.enabled = true;
// if we press the interaction button
if (Input.GetButtonDown (interactbutton))
{
// if it is a door
if (hit.collider.CompareTag ("Door"))
{
//open the door
hit.collider.GetComponents<Door>().ChangeDoorState();
}
}
}
}
else
{
interacticon.enabled = false;
}
}
}
Answer by Cherno · Jun 18, 2017 at 11:44 PM
In this line:
hit.collider.GetComponents<Door>().ChangeDoorState();
You use the GetComponents command, which returns an array of all found Components of the specified Type. Keeping this in mind, the error message tells you exactly what you need to know: The array of Type Door[] itself doesn't have a function "ChangeDoorState". Only the Type "Door" has this, so you have to first access one of the array's elements, like this:
hit.collider.GetComponents<Door>()[0].ChangeDoorState();
However, since I assume that no collider will have more than one Door script on it, it's probably better to use the GetComponent command:
hit.collider.GetComponent<Door>().ChangeDoorState();