- Home /
Declare Component type in GetComponent
Hello,
So I am trying to access scripts from a couple of other objects, and it is working fine in one case, but not in another, and i'm doing exactly the same thing!?!?
private var cam : Camera;
private var player : Transform;
private var gyroScript : gyroCam;
private var tapScript : TapMoveDragLook;
function Start(){
cam = Camera.mainCamera;
gyroScript = cam.GetComponent(gyroCam);
player = transform.Find("Player");
tapScript = player.GetComponent(TapMoveDragLook);
}
The gyro related stuff works fine, but it says that 'TapDragMoveLook' does not denote a valid type, but gyroCam is a valid type either is it, how can one be and one not?
If i change the type to component I can access it it seems, but not enable/disable.
I'm stumped and would love some help!!
Thanks heaps.
Answer by Bunny83 · Jun 27, 2012 at 11:32 AM
There are only a few possibilities:
The other script is C# and therefore can't be accessed by UnityScript unless it's compiled first (so the c# script is in an earlier compilation group).
Both scripts are UnityScript (i guess they are after your reply) and you placed them in seperate compiling groups, so they can't see each other.
The last thing would be you mistyped the name. The script name has to be exactly the same. It's case-sensitive
TapMoveDragLook
would not be the same asTapMoveDraglook
for example.
The compiling groups can be irritating when you don't know about them. Read this page carefuly: script compilation groups.
Ahhh!!
I didn't even think about compilation groups because i wasn't accessing anything within the script, just enabling/disabling. Tap$$anonymous$$oveDragLook is C#, so i popped it in the plugins folder and all is good.
I've gotta learn C#, all this switching between languages, not being able to call out from previously compiled scripts, and issues like this, are just headaches i don't need.
Thanks for your help man.
Answer by Mizuho · Jun 27, 2012 at 08:42 AM
TapMoveDragLook
refers to your own custom class. If it's not a valid type, then you may not have the class declared correctly.
public class TapMoveDragLook : MonoBehaviour {
Should be at the top of your file declaring this class.
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html
Hey
Thanks for the reply.
I haven't declared a new class for gyroCam though, it's just the name of a script attached to the camera. I', using js, the declaration you mentioned is c# right?
I'm really just doing this from the document you linked to
// This finds the script called OtherScript in the same game object
// and calls DoSomething on it.
function Update () {
var otherScript: OtherScript = GetComponent(OtherScript);
otherScript.DoSomething();
}
So here "OtherScript" is the equivalent to my"TapDrag$$anonymous$$oveLook", as is gyroCam.
@siberman: Don't post comments as answers. Answers should answer the question and shouldn't be a reply to someone elses post.
I've converted your answer into a comment.
Feel free to answer your own question when you found your problem and none of the existing answers solved your problem. Don't forget to tick an answer to mark the question as solved.