- Home /
Cant acces function in other script
Hi i am trying to acces function in other script on other object but i am getting this error: Assets/selecting.js(12,32): BCE0019: 'ToggleSelection' is not a member of 'UnityEngine.Component'. When trying to call a function ToggleSelection. What am i doing wrong?
First script:
#pragma strict
var hitInfo : RaycastHit;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hitInfo, Mathf.Infinity) && hitInfo.transform.gameObject.tag == "Unit")
{
var script = hitInfo.transform.gameObject.GetComponent("ToggleSelected");
script.ToggleSelection();
}
}
}
Second script:
#pragma strict
public var selected : System.Boolean = false;
public function ToggleSelection()
{
if(selected == false)
{
selected=true;
}
else
{
selected=false;
}
}
Comment
Best Answer
Answer by jeffreyrampineda · Jan 10, 2015 at 08:27 AM
var script = hitInfo.transform.GetComponent(ToggleSelected);
script.ToggleSelection();
Try removing the string "" quotation marks from the first script like this.
Answer by Ericool · Jan 10, 2015 at 02:49 AM
try to replace this line
var script = hitInfo.transform.gameObject.GetComponent("ToggleSelected");
by
var script = hitInfo.transform.gameObject.GetComponent("ToggleSelected") as ToggleSelected;
Your answer
Follow this Question
Related Questions
On carachterspawn level load error? 0 Answers
Error with Camera WorldtoScreenPoint 1 Answer
Error in my script i don't understand. 0 Answers
Unity Gui lable problem with showing js var using C# 2 Answers
script error 1 Answer