- Home /
Calling individual variables from individual scripts
In the Inspector each script has a different pair of values for Note1aTexture and Note1bTexture but in-game I can only ever pull up one texture -- no matter how specific I am about referring to this script that's being run from the object that just collided with the player. What am I doing wrong or failing to comprehend?
private var hasCollided: boolean = false;
private var usedState: boolean = false;
var labelText: String = "";
var Note1aTexture: Texture;
var Note1bTexture: Texture;
var pickupSound: AudioClip;
var pgWidth = Screen.width/2;
var pgHeight = Screen.height/1.5;
var pgW2 = pgWidth/2;
var pgH2 = pgHeight/2;
var pgX = 0;
var pgY = 0;
var pgMargin = 32;
function Update(){
if (hasCollided == true){
if (Input.GetKeyDown(KeyCode.E)){
if (this.usedState == true){
Destroy( transform.root.gameObject );
}
if (this.usedState == false){
this.usedState = true;
print("Player picked up item.");
audio.PlayOneShot(pickupSound);
if (tag == "Note"){
print("It was a note.");
}
}
}
}
}
function OnGUI()
{
if (usedState == true){
GUI.DrawTexture(Rect(pgX,pgY,pgWidth,pgHeight),this.Note1aTexture,ScaleMode.StretchToFill,true,0.0f);
GUI.DrawTexture(Rect(pgX+pgMargin*2,pgY+pgMargin,pgWidth-pgMargin*8,pgHeight-pgMargin),this.Note1bTexture,ScaleMode.StretchToFill,true,0.0f);
GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),("Press E to add to Journal; Space to put it down."));
if (Input.GetKeyDown(KeyCode.Space)){
this.usedState = false;
audio.PlayOneShot(pickupSound);
}
}
if (this.usedState == false){
if (this.hasCollided ==true){
GUI.Box(Rect(140,Screen.height/2,Screen.width-300,32),(labelText));
}
}
}
function OnTriggerEnter(c:Collider){
if(c.gameObject.tag =="Player"){
this.GetComponent.<PickUpNote1>().hasCollided = true;
}
}
function OnTriggerExit( other : Collider ){
this.GetComponent.<PickUpNote1>().hasCollided = false;
}
Just FYI, you can't call variables, unless they point to a function :)
This should work, what exactly isn't going to plan?
BTW If you have 2 scripts of the same type attached, bad things generally happen
I have the same script attached to multiple objects. When I try to get information from one of those scripts, filtered by the collision volume the player is touching, I ins$$anonymous$$d get whichever the engine decides (which I believe is always the last instance of the script).
For instance, obviously hasCollided should only become true on the object which has been collided with; unfortunately it's being set to both scripts. I need some means to discri$$anonymous$$ate both ways here but I don't know of any.
Answer by darthbator · Jun 04, 2013 at 12:17 AM
This is JS? In C# when you say this.var or this.method you are saying basically "my instance, the one I am executing right now". What texture are you trying to get? Are you trying to grab a texture stored on a script in the item you are picking up?
Correct. I'm wanting to draw the textures defined in the Inspector of the script attached to the object the player has collided with.
Ins$$anonymous$$d, I'm only getting one script's textures and both scripts are getting set whenever only one should when the player collides with the trigger volume.