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
0
Question by CrilleStyles · Nov 02, 2014 at 01:04 PM · scorescoring

BCE0019: 'GetComponent' is not a member of 'Object'.

I get this error when I tried to add score on death. how can I fix it? I'll type in the enemy health script and the score script. Here we go.

pragma strict

var thisObject : Transform; var Health = 100; var WillDrop : boolean = false; var ObjectToDrop : GameObject[]; //[] means that you can use more than one objects var otherObj = new Array(); var isDead : boolean = false;

 function Start()
 {
 WillDrop = Random.value < 0.10; //Dropchance of 10%
 }
 {
 otherObj[0] = GameObject.FindGameObjectWithTag ("Score");
 }
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
     
     if(Health <= 0)
     {
         isDead = true;
     }
 }
 
 function Dead()
 {
   if(WillDrop == true) {
         var obj : GameObject = ObjectToDrop[Random.Range(0,   ObjectToDrop.length)]; //Randomize a prefab to drop between   Medkit, ammobox and AllInOne kit  
         var pos: Transform = thisObject;
         Instantiate(obj, pos.position, pos.rotation); //Spawn prefabs
         Destroy (gameObject);
  }
  
     if (WillDrop == false) {
         Destroy (gameObject);
     }
     
 }
 
 function Score()
 {
 otherObj[0].GetComponent(ScoreSystem).curScore += 10;
 }
 
 
 function Update()
 {
 if(isDead == true)
     {
     Score();
     Dead();
     }
 }


 #pragma strict
 
 var Score = 0;
 var curScore = 0;
 var Style : GUIStyle;
 
 function Update () {
 
 if (curScore > Score)
 {
 Score++;
 }
 if (Score > curScore)
 {
 Score = curScore;
 }
 }
 
 function OnGUI()
 {
 GUI.Box(Rect(0,0,100,25), Score.ToString(), Style);
 }



Comment
Add comment · Show 3
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 Subzero619 · Nov 02, 2014 at 01:17 PM 0
Share

Try to put ScoreSystem in quotes.

avatar image CrilleStyles · Nov 02, 2014 at 02:42 PM 0
Share

I tried that. didn't work

avatar image Eric5h5 · Nov 02, 2014 at 07:44 PM 0
Share

Never put the type in quotes; that only causes problems.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Bunny83 · Nov 02, 2014 at 07:30 PM

To all that answered this question with an advice to use a different GetComponent version, have you actually read the error in the title? If you get an error that "something" is not a member of "somethingElse" that means that "somethingElse" is the problem.

The problem here is that your otherObj variable is of type Array. The Array class is a container class which stores objects without their type. Every item in that collection is stored as System.Object. When you access an element you would need to cast it back to the actual type.

However it's usually better to not use the Array class at all. Either use a built-in array or if the size of your collection will change a lot at runtime, use a generic List. See the Which_Kind_Of_Array_Or_Collection_Should_I_Use wiki page.

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 Eric5h5 · Nov 02, 2014 at 07:44 PM 0
Share

This is the correct answer...never use Array. Under any circumstances. It's pointless and should really be removed because it only causes problems like this.

avatar image tanoshimi · Nov 02, 2014 at 07:57 PM 0
Share

Ha ha - well spotted. We see so many questions with people using GetComponent you tend to reel off the same answer on autopilot. $$anonymous$$y bad. Guess I'll stick to answering questions about C# in the future :)

avatar image Kiwasi · Nov 02, 2014 at 11:44 PM 0
Share

You can go one step further, as otherObj[0] is the only item ever accessed from the collection, why make otherObj a simple GameObject ins$$anonymous$$d? Change line 5 to:

 var otherObj : GameObject;
avatar image
-1

Answer by 767_2 · Nov 02, 2014 at 02:08 PM

you can do it like

 otherObj[0].GetComponent.<ScoreSystem>().curScore += 10;



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 CrilleStyles · Nov 02, 2014 at 02:41 PM 0
Share

I still get the same error for some reason

avatar image Eric5h5 · Nov 02, 2014 at 07:43 PM 0
Share

There's no reason to do that and a couple of reasons not to.

avatar image 767_2 · Nov 07, 2014 at 11:39 AM 0
Share

this the generic version of getcomponent in js and it is very much used , and @Eric5h5 i dont know why you say it shouldnt be used

avatar image
-1

Answer by tanoshimi · Nov 02, 2014 at 02:11 PM

GetComponent returns an object. You need to cast the result to a ScoreSystem before accessing the curScore member (or, better yet, use the generic version of GetComponent).

Comment
Add comment · Show 4 · 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 CrilleStyles · Nov 02, 2014 at 02:17 PM 0
Share

generic version?

avatar image tanoshimi · Nov 02, 2014 at 02:41 PM 0
Share

http://docs.unity3d.com/$$anonymous$$anual/GenericFunctions.html

avatar image Eric5h5 · Nov 02, 2014 at 07:42 PM 2
Share

This is Unityscript, not C#. Don't use the generic version, it's a bit slower and uglier and serves no purpose.

avatar image tanoshimi · Nov 02, 2014 at 07:57 PM 0
Share

Oh really? Thanks for correction. I'm a C# person so didn't know that! :)

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score System to keep track of Points (Please remove) 1 Answer

Save high score and coins to Space Shooter game 1 Answer

How to save score for survival time? 1 Answer

Score Not Adding Up..? 1 Answer

Scoring System 3 Answers


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