• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by siberman · Jun 27, 2012 at 08:38 AM · componentaccesstype

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 as TapMoveDraglook for example.

The compiling groups can be irritating when you don't know about them. Read this page carefuly: script compilation groups.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siberman · Jun 27, 2012 at 12:40 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siberman · Jun 27, 2012 at 11:04 AM 0
Share

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.

avatar image Bunny83 · Jun 27, 2012 at 11:35 AM 0
Share

@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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Very simple component-access question 0 Answers

Can I access components scripts? 3 Answers

Painted Tree's Script Component 1 Answer

Access to variables from other script objects 3 Answers

Unity crashes when accessing a function of a script from another script 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges