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 Nk.Andrei · Sep 24, 2012 at 02:03 PM · bce0019

beginner syntax error..

"BCE0019: 'xxxx' is not a member of 'UnityEngine.Component'."

I am currently trying to follow some basic tutorials for a better understanding of scripting within unity in javascript but recently I have come across an error witch I have been unable to solve. In this case I am trying to access a variable from one script to use in a second one by using the GetComponent function but it always gives me an error anytime I use the GetComponent function be it a variable or even if i try to access other functions(I get the same error).

Example

 // player script
 var lives                    : int    = 3;
 var playerSpeedHorizontal    : float = 10.0;
 var playerSpeedVertical      : float = 10.0;
 var horMin                   : float = -4.0;
 var horMax                   : float = 4.5;
 var verMin                   : float = -3;
 var verMax                   : float = 2.7;
 var projectile               : Transform;
 var sockets                  : Transform;
 
 function Update ()
 {
     var transV : float = Input.GetAxis("Vertical")*playerSpeedVerticalTime.deltaTime;
     var transH : float = Input.GetAxis("Horizontal")playerSpeedHorizontal*Time.deltaTime;
     
     transform.Translate(transH,transV,0);
 
     transform.position.x = Mathf.Clamp(transform.position.x,horMin,horMax);
     transform.position.y = Mathf.Clamp(transform.position.y,verMin,verMax);
 
     if(Input.GetKeyDown("space"))
     {
         Instantiate(projectile,sockets.position,sockets.rotation);
     }
 }
 
 
 //script asteroid
 
 var asteroidSpeed : float = 6.0;
 var explosion : Transform;
 
 function Update ()
 {
     transform.Translate(Vector3.downasteroidSpeedTime.deltaTime);
     if(transform.position.y <= -5)
     {
         transform.position.y = 5;
         transform.position.x = Random.Range(-4.0,4.5);
     }
 }
 
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.gameObject.tag == "player")
     {
         other.GetComponent("script player").lives-=1;
     }
     ResetEnemy();
 }
 
 
 function ResetEnemy ()
 {
     transform.position.y = 5;
     transform.position.x = Random.Range(-4.0,4.5);
 }

In this example I am trying to substract the variable lives found in script one accessing it with script two by using the GetComponent function.But when it compiles it gives me the BCE0019 error.This error also appears even if i create a separate function to substract the variable lives.

Any help and an explanation would be appreciated.

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 Eric5h5 · Sep 24, 2012 at 04:22 PM 1
Share

Format your code.

avatar image Mander · Sep 24, 2012 at 04:41 PM 0
Share

make public ur variable lives at scriptplayer.js

avatar image Bunny83 · Sep 24, 2012 at 04:52 PM 0
Share

@$$anonymous$$ander: It is public. In UnityScript the default visibility is public. In C# it's private.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Hybris · Sep 24, 2012 at 04:35 PM

 other.GetComponent("script player").lives-=1;

that should be without the "" so like this:

 other.GetComponent(script player).lives-=1;

but that might make some errors and that is because the name of the script has a space in between, try to avoid spaces, what I like to do is use underscores so the name would be: script_player or what you could do is use capitals like ScriptPlayer, personally I would've named it PlayerScript so:

 other.GetComponent(script_player).lives-=1;

Oh, and please format your code, it was unreadable like that, formatting is like using tabs and stuff.

-Hybris

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 Bunny83 · Sep 24, 2012 at 04:51 PM 0
Share

exactly, script names (which are class names) can't include spaces. Also when using the string version of GetComponent, the compiler doesn't know the actual type of the component. When using the "type" version, the compiler knows the component that is returned be GetComponent is of this specific type.

avatar image Eric5h5 · Sep 24, 2012 at 09:38 PM 1
Share

Script names can technically include spaces, and they will work, however you will not be able to write GetComponent($$anonymous$$y Script). Ins$$anonymous$$d you'd have to use quotes: GetComponent("$$anonymous$$y Script"). So I'd recommend not ever using spaces in script names.

avatar image Bunny83 · Sep 25, 2012 at 12:55 AM 0
Share

They really work? What class name is generated for such scripts?

Another thing i can put on my "list of things i have to test someday" :D

avatar image Eric5h5 · Sep 25, 2012 at 01:36 AM 0
Share

They work in Unityscript, anyway. The class name is the same as the script name, so it will have a space in it. Not that you'd want to, of course....

avatar image
0

Answer by CrayOn · Jul 27, 2013 at 05:12 PM

It's a casting problem.

(other.GetComponent(scriptPlayer) as scriptPlayer).lives -=1;

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 Eric5h5 · Jul 27, 2013 at 11:25 PM 0
Share

The question is using Unityscript, so the correct way is

 other.GetComponent(scriptPlayer).lives -= 1;

You don't need to use "as X" in Unityscript, and the generic version is unnecessary (and slightly slower).

avatar image CrayOn · Jul 28, 2013 at 01:38 AM 0
Share

Hmm i don't know i couldn't get things done until i did it like in the comment.

Why i can not post comment like this (without ') it is not allowing? other'.'GetComponent'.'<'scriptPlayer'>'()'.lives -=1;

avatar image Eric5h5 · Jul 28, 2013 at 01:50 AM 0
Share

The text parser on this site has issues, and the tags cause stuff to get eaten unless you use spaces. other.GetComponent< scriptPlayer >().lives -= 1;. If you're using C#, feel free to do that or use the "as scriptPlayer" casting, but as I mentioned there's no reason to do any of that in Unityscript.

avatar image CrayOn · Jul 28, 2013 at 02:11 PM 0
Share

Thank you, i will use spaces... I am using JavaScript for coding.

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

14 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

Related Questions

Some problems after switch the game for iOS mode 2 Answers

error for array functions 2 Answers

finish line script 3 Answers

transform.position not a member??? 2 Answers

I'm attempting to make an array of Gameobjects with javascript but when I want to transform an object in the array, I get an error 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