- Home /
change variables in another object's script's instance
i have these ray cast 'checker' cubes, called CubeBouncerJH and CubeBouncerKU, that check if they are surrounded by other cubes tagged 'Rcube', i implemented this by the following code:-
if(Input.GetKey("k")) { var hitK: RaycastHit;
//check if we're colliding
if(Physics.Raycast(transform.position, transform.right, hitK, 1))
{
//...with an object
if(hitK.collider.gameObject.tag =="Rcube")
{
hitK.collider.gameObject.transform.position.y=15;
}
}
}
now, for testing purposes, i used that 'transform.position.y=15' line to just bump the cube getting hit by the ray up to a different height, to check that im being able to affect properties of the cubes surrounding the checker cubes.
this is code in the 'update' function of the 'checker' cubes' script (they sit back to back so i can check in all 4 directions, eventually I'll be putting up 36 small cubes at 10 degree intervals to create an unavoidable circle of raycasting checkers.
this code worked perfectly, now, to rotate the cubes, im using another script, attached to each and every one of the 'rotatable' cubes surrounding the 'checkers'
at this point, i'd like to point out that the purpose of this code is to eventually facilitate rotation of individual slices of a rubik's cube sort of structure, and these checker cubes are to be invisible 'Slice confirmers' that stick around on the inside of the cube
the script attached to the rotatable cubes is called 'rotationScriptKeyLess.js':-
var willRotateHorizontallyNow: boolean; var willRotateVerticallyNow: boolean; var RotAxis : Vector3; var MyAngle = 0; var to : Transform;
var rotspeed = 0;
var CenterOfRotation;
var anglecounter=0;
function Update () { if ( Input.GetKey("a") && willRotateHorizontallyNow == true) { if(MyAngle ==0) { RotAxis = Vector3.up; MyAngle=90; CenterOfRotation = GameObject.Find ("CubeBouncerJH"); rotspeed = 5; } }
if ( Input.GetKey("d") && willRotateHorizontallyNow == true)
{
if(MyAngle ==0)
{
RotAxis = Vector3.up;
MyAngle=90;
CenterOfRotation = GameObject.Find ("CubeBouncerJH");
rotspeed = -5;
}
}
if(anglecounter<MyAngle) { transform.RotateAround (CenterOfRotation.transform.position, RotAxis ,rotspeed);
anglecounter=anglecounter+Mathf.Abs (rotspeed);
}
else
MyAngle=0;
if(MyAngle == 0)
anglecounter=0;
}
this code works too, if i choose the cubes individually, and 'check' their 'willRotateHorizontallyNow' checkboxes to manually set the variables for their instance of the script to '1' in the inspector.
what i need, is for the value of their instance to get changed by the raycaster i made, the last bit of code i tried for the same was:-
if(Physics.Raycast(transform.position, transform.right, hitK, 1))
{
//...with an object
if(hitK.collider.gameObject.tag =="Rcube")
{
hitK.collider.gameObject.rotationScriptKeyLess.willRotateHorizontallyNow=true;
}
}
and well, it didn't work...
i know that the raycast is correctly hitting the cube, i know that the cube will do exactly what i want it to do once it's boolean value is changed, i just don't know how to change that value as a result of the raycast.
now, hopefully this is all clear enough, i need a solution, or a suggestion, anything! to make this code better, and more importantly, work) any ideas?
thank you :)
Answer by · Oct 01, 2010 at 11:35 AM
To make it work, it seems like you're just missing GetComponent.
hitK.collider.gameObject.GetComponent(rotationScriptKeyLess).willRotateHorizontallyNow=true;
I hope this solves your problem!
Taking a quick scan across the rest of your code, the only thing I'd suggest is to explicitly declare the type of all of your variables. You've done it for some, but you could safely do it for the rest. (e.g. var rotspeed : int = 0;)
i have dabbled with getcomponent a LOT, what i've realized is (correct me if im wrong) you might be able to change the value of variables in a script attached to the same object with getcomponent, but it just doesn't work the same way for another object's scripts, any other suggestions?
i used your code, i've used it before too, didn't work :-\
actually, i just got an idea, i'll just make flag script in the checker box and ask the rotatable cubes to check that variable, let's see :) thanks!
GetComponent works fine to get the component (script) on the specified gameObject. hit$$anonymous$$.collider.gameObject specifies which object you should be looking for the scripts in - in this case, the one that was hit by the raycast - and will find the component (script) on that.
it still didn't work for me, wait, is there anything wrong with the placement of the code? my way of debugging is selecting the object before hitting 'play' and then observing the values change in the inspector...is the variable type (public var Vname : boolean) ok?
i'd used a 'static' variable for one program earlier and it worked, but, im not sure if it's the right thing to do here!!
A static var will be unique project-wide, so it wouldn't work for your situation. It shouldn't matter where you place that code - as long as the raycast is correctly hitting the gameObject, and that gameObject is tagged "Rcube" and the GetComponent value is exactly the same as the script name, it should all just work. Is there somewhere you could upload the project so I can test it locally?