• 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 Romano · Mar 08, 2014 at 05:18 PM · script errormonobehaviour

Generic type for script variables.

Hey there,

this is my current code (js):

 public var cameraMovementScript : MonoBehaviour;
 
 function Start()
 {
    cameraMovementScript = cameraMaster.GetComponent(CameraMovement);
    cameraMovementScript.autoCameraMove = true;
 }

But I get the error: 'autoCameraMove' is not a member of 'UnityEngine.MonoBehaviour'.

autoCameraMove IS a variable in CameraMovement though. And if all scripts in javascript automatically derive from MonoBehaviour then what's the problem? I know that if I changed the type to CameraMovement that would work. But I would like to know how to keep it generic.

Thankyouverymuch!

Romano

Comment
Add comment · Show 4
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 Jamora · Mar 08, 2014 at 05:44 PM 0
Share

You have the idea of inheritance reversed. Only (public and protected) variables in the parent class can be found in the child class. This doesn't work the other way around.

avatar image Romano · Mar 08, 2014 at 05:49 PM 0
Share

Thanks Jamora, but I think I understand inheritance ok. I'm not trying to access a variable belonging to the parent class. I'm trying to access variable belonging to the child class. It's not a question about how inheritance works. It's about whether or not $$anonymous$$onobehaviour can act as a generic type for other scripts.

avatar image Jamora · Mar 08, 2014 at 05:54 PM 0
Share

$$anonymous$$onoBehaviour can behave as a parent type for classes inherited from it, but you can only access variables and methods that are defined in the $$anonymous$$onoBehaviour class, because you're defining your type as a $$anonymous$$onoBehaviour (line 1).

The type after the colon deter$$anonymous$$es which methods and variables are available to you, regardless of the actual type of the object being referenced.

avatar image Romano · Mar 08, 2014 at 06:02 PM 0
Share

I see. So I can extend $$anonymous$$onoBehaviour and add more variables to the derived class so long as I don't define that class as a $$anonymous$$onoBehaviour somewhere else and then try and access the new variables from that same place?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jamora · Mar 08, 2014 at 07:21 PM

Yes. The compiler only knows the type you define after the colon in UnityScript and uses that type to see what variables or methods you can access.

The way to keep things generic is to create your own parent class that has all the functionality you need:

 public class MyCamera extends MonoBehaviour{
     var autoCameraMove :boolean = false;
 
     function FocusCamera(){ Debug.Log("Focusing camera...");}
 }
 
 public class AMoreSpecificCamera extends MyCamera{
     var specificMove : boolean;
     function FocusCamera(){Debug.Log("A specific Camera with autoMove "+autoCameraMove);}
 }

Now, code such as:

 var cam : MyCamera;
 cam = GetComponent("AMoreSpecificCamera");
 cam.FocusCamera();
 //prints "A specific Camera with autoMove false"

but

 var cam : MyCamera;
 cam = GetComponent("AMoreSpecificCamera");
 Debug.Log(cam.specificMove); //error, because specificMove is in  AMoreSpecificCamera, not MyCamera
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 Romano · Mar 12, 2014 at 11:08 AM 0
Share

Thanks a million :D

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

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

Related Questions

Can anyone provide some help with that error ? 1 Answer

no monobehavior script in file even after a new project is created eveytime the same error 1 Answer

How can I reference a specific script inside a GameObject from another MonoBehaviour. 2 Answers

Get OnMouseUp event in other object's script 1 Answer

Quick question about GetComponentsInChildren< MonoBehaviour >() 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges