- Home /
BCE0005: Unknown identifier: 'transform'
I have looked around to find an answer to my problem but have not come across any, so I'm sorry if I did overlook it. My script was working in the previous versions of Unity but ever since I did a recent update (I've been absent for a while) a lot of scripts have errors written all over them. Here is the script...
//class that defines the physical properties and attributes of the cannonball
class cannonBlast {
var cannonball : GameObject; //create a slot to assign the cannonball prefab to
var blastParticle : ParticleEmitter; //create a blast particle slot to assign a particle emitter
var cannonLight : Light; //create a light slot to assign a light prefab for when the blast occurs
var spawnPos : Transform; //create a slot to assign an empty game object as the point to spawn the cannon
var shooting : boolean = false; //a boolean we can use to stop the player from shooting during the actual shot
}
var cannon : cannonBlast; //a varible used to access all other variables inside of the connected class (essentially a group)
//class that defines the mechanics of the cannonball
class cannonballMechanics {
var fireRate : float = 0.5; //a variable setting the rate of fire
var reloadRate : float = 1.5; //a variable setting the time for reload
var force : int = 1000; //the cannons maximum power
@System.NonSerialized
var nextFire : float = 0.0; //a variable used to determine when to fire the next cannon
}
var mechanics : cannonballMechanics; //a varible used to access all other variables inside of the connected class (essentially a group)
//inspector variables
//list of default variables that can be tweaked, selected, and changed inside the inspector
var instanceCannonball : GameObject; //a variable holding an instance of the cannonball (which is duplicated upon firing)
function Start () {
}
function FixedUpdate () {
Shoot(); //function put inside FixedUpdate because it handles movement over time
}
//function to create and shoot the cannonball
function Shoot () {
if (Input.GetMouseButton(0) && Time.time > mechanics.nextFire) { //checks if the user clicks the left mouse button
instanceCannonball = GameObject.Instantiate (cannon.cannonball, cannon.spawnPos.position, cannon.spawnPos.rotation); //creates a duplicate of the cannonball
//at the applied position and rotation
mechanics.nextFire = Time.time + mechanics.fireRate;
instanceCannonball.rigidbody.AddForce (transform.forward * mechanics.force); //add - or + force to the duplicated cannonball
Debug.Log ("Fire on Target!");
cannon.shooting = true; //shows if the user is currently shooting
} else {
cannon.shooting = false; //shows if the user is not shooting
}
}
The error is BCE0005: Unknown identifier: 'transform'. Located inside the shoot function, it's the line: **instanceCannonball.rigidbody.AddForce (transform.forward mechanics.force);* I just don't see what's wrong with it. instanceCannonball was already declared and the variable used for adding force is declared in the class cannonballMechanics, referenced by mechanics...
Anyway, I had the same problem earlier with the error, BCE0005: Unknown identifier: 'Instantiate', located inside the shoot function, the line: instanceCannonball = GameObject.Instantiate (cannon.cannonball, cannon.spawnPos.position, cannon.spawnPos.rotation); Prior to the update I did not have to put (GameObject.) in front of Instantiate, but I checked a recent post stating the devs changed the coding. Java is more strict on defining variables. Problem is. I dont know how to edit the (transform. forward) to fix the error.
Any help or suggestions would greatly be appreciated. I'll still be attempting...
Briefly looking at your code I can't see an obvious reason for transform to be unavailable so I'll just explain what is happening when you use "transform". Using "transform" directly references the transform component on the object in which the script is attached. Try cacheing the transform in a variable then accessing it. var tr = gameObject.transform;
Answer by piko · Jun 08, 2012 at 05:06 AM
Sorry guys; I have figured out the issue. The code itself works but I received an error because the script bearing the code was named 'cannonBlast', which, is also, the name of a class inside the script.
So I guess Unity tried to access the script--which is a class itself when using java--against the class inside of the script. I figured it out when arkon tried pasting my code into an empty js file; I did the same thing--obviously changing the script name--and it worked.
Thanks a lot for your help on this one...
Answer by arkon · Jun 07, 2012 at 08:21 AM
I've just pasted your code into an empty javascript file in my game and it all works fine with no errors. I suspect it's something to do with where you have the file inside your assets folder. I put it here and it all works fine.
Game->Assets->Scripts