- Home /
Error when referencing one script from another.
I am getting an error when attempting to access a method in one script from another script. I asked the question here and was told to ask the question differently.
Alright, I'm using both JS and C# because I'm transitioning to C#, but as I understand it the solution should be almost exactly the same, so, here I go:
animationManager.cs
using UnityEngine;
using System.Collections;
public class animationManager : MonoBehaviour {
// Make this game object and all its transform children
// survive when loading a new scene.
void Awake () {
DontDestroyOnLoad (transform.gameObject);
}
public void sayHello() {
Debug.Log ("hello");
}
}
I have this script attached to a gameObject named "_globalScripts", which is active in my game.
shipBehavior.js
var kAnimMan : animationManager;
function Start () {
var kGloMan = GameObject.Find("_globalScripts");
kAnimMan = kGloMan.GetComponent(animationManager);
kAnimMan.sayHello();
}
This script is attached to my player's ship. The script works otherwise and performs a number of actions.
Now, there may be some debate about the code I use inside of Start(), which is what happened the last time I wrote this question out. However, that is of little consequence because the script fails waaay before it arrives there. It actually fails on the kAnimMan declaration, with this error:
Assets/Scripts/Ship/shipBehavior.js(30,16): BCE0018: The name 'animationManager' does not denote a valid type ('not found'). Did you mean 'UnityEditor.AnimationClipCurveData'?
What I'm doing seems fairly typical. I am creating a global animationManager class and trying to run that sayHello() method on it. But I've tried several things, read several articles, and browsed for answers here without any success.
Your help is appreciated.
Edit: Fixed by this answer
Answer by aldonaletto · Aug 24, 2012 at 02:40 AM
This is a recurrent problem that plagues users when mixing JS and CS: the compilers are different, thus they can't see each other during compilation. To solve this, move the CS script to Standard Assets or Plugins, and move the JS script to another folder in Assets (Assets/Scripts, for instance): Unity compiles scripts in Standard Assets, Plugins or their subfolders first, and scripts in other Assets subfolders last - once compiled, a script becomes pure CIL code and can be seen by other scripts, no matter in which language they are written.
Your answer
Follow this Question
Related Questions
Referencing a c# component script from JS script 2 Answers
How to reference another script and call a function in C# ? 2 Answers
Refin' another script in relation to speed, and not tacking from using the Update? 1 Answer
Trying to access a variable from another script attached to same object. 1 Answer
Multiple Cars not working 1 Answer