Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TheMorten · May 19, 2010 at 08:41 PM · errorgetcomponentfunctionbce0019

Why am I getting this error when accessing a variable or function on my script using GetComponent - 'x' is not a member of 'UnityEngine.Component'?

Hey,

I have a script in which I try to access the length of an array in another .js script. Frustratingly, an error keeps coming up, saying: 'myArray' is not a member of 'UnityEngine.Component'.

I've been trying to solve this for several hours and I'm at my wit's end...

The code looks something like this (ScriptA.js and ScriptB.js are components of the same object, 'Code'):

ScriptA.js

function Update () {
   if (GetComponent("ScriptB").myArray.length > 0) {
      //Execute some stuff
   }
}

ScriptB.js

var myArray = new Array();

function OnGUI () { if (someButton) { myArray.Push(new myClass); } }

myClass.js

class myClass {
   var intA : int;
   var intB : int;
}

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

1 Reply

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

Answer by duck · May 19, 2010 at 08:45 PM

You need to cast the result of GetComponent to the correct type. The correct type is the 'type' of your script - i.e. your script's name.

Common ways of doing it look like this:

function Update () {
   // put the reference into an explicitly typed var
   var thisScriptB : ScriptB = GetComponent("ScriptB");
   if (thisScriptB.myArray.length > 0) {
      //Execute some stuff
   }
}

or like this:

function Update () {
   // use 'as' to convert the type before using it:
   if ((GetComponent("ScriptB") as ScriptB).myArray.length > 0) {
      //Execute some stuff
   }
}
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 TheMorten · May 19, 2010 at 09:04 PM 0
Share

Thanks a lot, but I have one similar problem now...

I'm trying to use:

if (GetComponent("ScriptB").myArray.length > 0 && myRect.Contains(Input.mousePosition))

and it gives me the error:

'Contains' is not a member of 'Object'.

avatar image duck ♦♦ · May 19, 2010 at 09:17 PM 0
Share

You wouldn't happen to be getting 'myRect' out of your array would you? If so, you probably need to do the same casting shenanigans. Eg: `var myRect : Rect = myArray[n];`

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

No one has followed this question yet.

Related Questions

GetComponent Picking up multiple scripts from differrent objects? 1 Answer

problem with mathf function 2 Answers

Problems with Input.acceleration in Unity 4.1 1 Answer

What does this mean ? 1 Answer

Access a function of an instantiated prefab 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