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 gulpy3 · Jun 03, 2014 at 05:33 PM · getcomponentrandom.range

BCE0019 Error when trying to reference another scrip

So I have a script that picks a block to spawn based on RNG. I'm trying to get the value that is generated to be referenced in another script. I continually get this error though...

Assets/scripts/BlocDec.js(10,9): BCE0019: 'blockSpawn' is not a member of 'blockSpawn'.

I'd be ecstatic if someone could point me in the right direction..

random gen code "blockSpawn"

 #pragma strict
    
     var block1 : Rigidbody2D;
     var block2 : Rigidbody2D;
     
     function Update()
     {
     
     if (Input.GetMouseButtonDown(0))
     {
     var rand = Random.Range(1.0, 20.0);
        }
     if (Input.GetMouseButtonDown(0) && (rand < 10.0))
     {
         var clone = Instantiate(block1, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection(Vector3(0, 0, 0));
         Debug.Log(rand);
     }
     else if (Input.GetMouseButtonDown(0) && (rand > 10.0))
      {
     var clone2 = Instantiate(block2, transform.position, transform.rotation);
         clone2.velocity = transform.TransformDirection(Vector3(0, 0, 0));
      Debug.Log(rand);
     }
 
     
     }

Script that I'm trying to reference it i "BlocDec"...

 #pragma strict
 
 
 var rand: blockSpawn = GetComponent(blockSpawn);
 
 
 
 function Start () {
 
 if(rand.blockSpawn > 10){
 
 Debug.Log("Well ill be, it worked...");
 }
 }
 function Update () 
 {
 } 
 
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
0
Best Answer

Answer by Jeff-Kesselman · Jun 03, 2014 at 05:34 PM

Your problem is exactly what it says.

You are assigning the blockSpawn component to the rand variable in line 5.

You are then asking for a field called blockSpawn on the blockSpawn component in line 10.

But there IS no publicly accessible field called blockSpawn in your script for blockSpawn.

Comment
Add comment · Show 5 · 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 gulpy3 · Jun 03, 2014 at 05:50 PM 0
Share

Thanks for the help. I know my na$$anonymous$$g convention is terrible. I'm still really new to Unity and JS, so please bear with me. How would I go about adding a publicly accessible field that contains the value generated by var = rand in the blockSpawn script? Thanks a lot for the help and patience.. I know its frustrating sometimes helping the noobies.

Side question: Would it be beneficial in the long term if I jumped ship on JS and moved to C#? Or is it all about preference?

avatar image Jeff-Kesselman · Jun 03, 2014 at 06:05 PM 0
Share

Okay, second question first.

Yes, it would be very beneficial to jump ship to C#. C# gives you more hand holding and error checking then UnityScript does. I don't know of any shipped commercial Unity project that hasn't ended up converting to C# once they got over anything more then $$anonymous$$imal complexity. 9There probably are a few out there, I don't know about. Every rule has its exceptions.)

Now, in UnityScript (which is what people call javascript here... its not exactly javascript) you declare a public field by declaring a variable outside opt a function. block1 and block2 are public fields in your current script.

So you would define a new one like this:

 var blockSpawnNum:int;
 

Then yo would set it to your rand value linside of Update like so

 if (Input.Get$$anonymous$$ouseButtonDown(0))
 {
     var rand = Random.Range(1.0, 20.0);
     blockSpawnNum = rand;      
 }

Finally in your other script you would access it using rand (which is different then the rand in the other script) by rand.blockSpawnNum

This is a really good book to get you started on C# in Unity. Its what i use in my college level intro course:

http://www.amazon.com/Unity-Game-Development-Essentials-Goldstone/dp/184719818X

avatar image gulpy3 · Jun 03, 2014 at 06:35 PM 0
Share

You're awesome, Jeff. I'll be buying that book later and going through it. Hopefully by moving to C# I'll avoid more complicated problems when I start getting into more advance stuff.

avatar image tanoshimi · Jun 03, 2014 at 07:00 PM 0
Share

It's a good book, but just so you're aware that edition is five years old. There was a revised version published in 2011 (http://www.amazon.com/Unity-3-x-Game-Development-Essentials/dp/1849691444), but be aware that it's still written for Unity 3.x and, as we approach the release of Unity 5.x, expect several of the code samples to have changed and not necessarily to work "out of the box" on the latest version.

avatar image Jeff-Kesselman · Jun 03, 2014 at 07:02 PM 0
Share

We used it with 4.0 last spring and it was fine.

The only thing that was really out of date was the particle system info, and the author provided a youtube tutorial to replace that.

Now, its true it wont have the latest greatest features. And to be honest, I'm not in love with his coding style. He does a few things that work but are not great computer science.

But its still a good place to start I$$anonymous$$HO.

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

23 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

Related Questions

How do I use Get Component in this code 1 Answer

Neater way to test an Enum in a different script? 1 Answer

Problem calling variable across scripts (Closed) 1 Answer

i have variable in camera controller script which is target :transform i want it to change as i click on objects.and be that the same gameobject's transform i clicked as target.what do i do? 1 Answer

Having trouble with GetComponent 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