- Home /
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 ()
{
}
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.
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?
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
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.
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.
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