Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by Akashic · May 01, 2012 at 02:08 PM · javascriptobjectclassextend

Extending a class

Lets say i had a simple class:

 class Test extends UnityEngine.Object{
 
 private var pos : Vector3;
 
 function _get(){
 
      pos = GameObject.Find("Main Camera").transform.position;
      return pos;
 }

 }

 function Start () {

     var testClass : Test = new Test(); 
 
 print(testClass);       //prints Null
 print(testClass._get());//prints correct value
 }


The problem is that by extending the class - it always gets the value Null, but is actually not Null...

I dont know if "extends UnityEngine.Object" is correct, but it's the only way i could get "GameObject.Find" to actually work.

Any help would be appreciated.

Comment
Add comment · Show 2
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 Akashic · May 01, 2012 at 02:13 PM 0
Share

On an unrelated note - how exactly are we supposed to post code snippets here?

avatar image Tseng · May 01, 2012 at 02:16 PM 1
Share

There is a button to format code. Either that or add 1 tab or 4 spaces in front of the code.

Also what's exactly the reason you want to derive from UnityEngine.Object ins$$anonymous$$d of $$anonymous$$onoBehaviour

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tseng · May 01, 2012 at 02:20 PM

A look at the Unity3D Documentation could have solved your problem :)

.transform .rigidbody .collider

etc. are properties of the Component class (UnityEngine.Component). They do not exist in UnityEngine.Object.

If you want to attach your script to an object and access it's components (transform, rigidbody, colliders are all components) you must derive the script from component too.

http://unity3d.com/support/documentation/ScriptReference/Component

Usually all scripts which get attached to the GameObjects are derived from UnityEngine.MonoBehaviour. All default Unityscripts automatically derive from MonoBehaviour.

Comment
Add comment · Show 3 · 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 Akashic · May 01, 2012 at 02:29 PM 0
Share

well if i use UnityEngine.$$anonymous$$onoBehaviour ins$$anonymous$$d of UnityEngine.Object the class is still Null, thanks for the code format advice though :p

avatar image Tseng · May 01, 2012 at 02:38 PM 1
Share

Classes which derive from Component are not supposed to be initialized via constructor. They are automatically initialized from the Unity Engine when attached to an object.

But if you want to make class types (like you'd do in C#) you should actually derive from "System.Object".

 class Test extends System.Object 

would equal to C#

 public class Test {
 }
avatar image Akashic · May 01, 2012 at 02:41 PM 0
Share

System.Object did indeed solve the problem, thanks

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Class Casting US/JS - 'foo' is not a member of 'Object' 1 Answer

How to create an instance of a class stored in another script 0 Answers

Player Ship Collisions 1 Answer

Constructor called multiple times on Class 1 Answer

Trying to change a subclasses' variable 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