- Home /
How to access a variable in a script on the same object.
I am trying to get my script AmmoSwitch:
function Update ()
{
if( Input.GetButtonDown( "Fire2" )
isGlue = !isGlue;
}
to access the variable isGlue
, from the script GlueGunScript:
var Glue : Rigidbody; var Solvent : Rigidbody; var isGlue : boolean = true; var speed = 20; function Update() { if( Input.GetButtonDown( "Fire1" ) && isGlue) { var instantiatedGlue : Rigidbody = Instantiate( Glue, transform.position, transform.rotation ); instantiatedGlue.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); Physics.IgnoreCollision( instantiatedGlue. collider, transform.root.collider ); }
if( Input.GetButtonDown( "Fire1" ) && !isGlue)
{
var instantiatedSolvent : Rigidbody = Instantiate(
Solvent, transform.position, transform.rotation );
instantiatedSolvent.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedSolvent. collider,
transform.root.collider );
}
}
I tried to follow the lesson on the homepage, but I couldn't get it to work on my script, even when I looked at other ways. A script line would be greate! Thanks in advance!
Please try to learn about indenting from someone else's code. Your code is very hard to read because of your lack of indentation.
Answer by Jessy · Jan 03, 2011 at 04:53 AM
I read this but I didn't understand it. I do get that to access the script I use blank = GetComponent(GlueGunScript) but then I tried blank.isGlue = !isGLue but that didn't work, and that is really all I got from that page. $$anonymous$$ore help pls.
blank.isGlue = !blank.isGLue; isGLue does not magically become a variable associated with the script that has a reference to the instance of GlueGunScript. Also, don't name it GlueGunScript. It's obviously a script. GlueGun makes sense.
thanks, that worked. And I will work on my indentation.