- Home /
Duplicate Question
Calling Functions from another Script
Okay, so situation is, I have a charactermotor script(default unity package). I also have a Camera bobbing script.
For some reason this wont work! I want to say basically, if the character speed is greater than 2, show a debug. So I am calling a function such as this :
var bobscript : CamBob;
function Update () {
if(movement.maxForwardSpeed > 2.0) {
bobscript.DoSomething();
}
}
and then, my other script, (cambob script) :
function DoSomething ()
{
Debug.Log("DoSomething Active");
}
// this is just the function im trying to call, not the entire script.
I KEEP GETTING THIS ERROR : Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js(334,17): BCE0018: The name 'CamBob' does not denote a valid type ('not found').
Is the name of your Camera bobbing script exactly "CamBob.js"? If your script file name is lowercase "cambob.js", than you need to match the case exactly.
Answer by ransomink · Mar 23, 2014 at 06:37 PM
You are referencing the script properly, but inside the inspector, you must add/drag the GameObject (using the CamBob script) into the bobscript (empty slot).
Make sure your spelling and casing is correct for your script. As whebert mentioned above, you must match the case exactly, so it's either: "CamBob.js" or "cambob.js" (basically whatever you named it).
Answer by Fappp · Mar 24, 2014 at 09:38 PM
var bobscriptGo : GameObject;
function Update () {
if(movement.maxForwardSpeed > 2.0) {
bobscript.SendMessage("DoSomething", 1);;
}
}
and then in bobscript:
function DoSomething (sentVar : int)
{
Debug.Log("DoSomething Active" + sentVar);
}
This is way slower than GetComponent. You should consider this way when it makes GetComponent complicated, else avoid.
Answer by ransomink · Mar 25, 2014 at 06:42 AM
Cambob script
public function DoSomething ()
{
Debug.Log ( "You should be doing something...");
}
Script that calls the function
// this variable will show up in the inspector
// place the GameObject that has the Cambob script attached to it inside the variables' field
public var bobScript : Cambob;
function Update ()
{
bobScript.DoSomething ();
}
You can also reference the Cambob script through code so you don't have to use the editor, but this method is slower.
// I made it private because there is no need to use/change it inside of the editor
// (unless you want to check if the correct GameObject appears when executed).
private var bobScript : Cambob;
function Awake ()
{
bobScript = GameObject.Find ( "The GameObject Name" ).GetComponent ( Cambob );
}
function Update ()
{
bobScript.DoSomething ();
}
Follow this Question
Related Questions
Script not function if mouse is on object 1 Answer
How to access variable from another function? 2 Answers
Destroy object question! 3 Answers
BCE0044: expecting (, found 'OnGUI'. 1 Answer
Pan Orthographic Camera 0 Answers