- Home /
Change Variable on Another Script
I have 2 scripts running in my game, one attached to a transform (spawnHandler) called "RandomSpawnRandomLocation"
#pragma strict
var spawnPoint : Transform[]; // Create list of possible spawn locations
var wepType : GameObject[]; // Creat list of possible weapons to be spawned
var wepPresent : boolean = false;
function Start()
{
SpawnWeapons(); //Run SpawnWeapons coroutine
}
function Update()
{
if(wepPresent == false) //Check to see if weapon is present
{
SpawnWeapons(); //If no weapon is present run SpawnWeapons coroutine
}
}
function SpawnWeapons() //Spawns a random weapon at one of possible spawn locations
{
var randomLoc : int = Random.Range(0,4); //Pick random number between 1 and 4
var randomWep : int = Random.Range(0,4); //Pick random number between 1 and 4
Instantiate(wepType[randomWep], spawnPoint[randomLoc].position, spawnPoint[randomLoc].rotation);
//Pick random weapon depending on which number was generated by 'randomWep'
//Spawn weapon at location depending on which number was generated by 'randomLoc'
wepPresent = true;
}
The other is a simple self destruct script attached to the gameObjects/prefabs that get spawned - "DestroySelfOnContact" (the objects this script is attached to are prefabs and get spawned/destroyed multiple times)
#pragma strict
function OnTriggerEnter()
{
Destroy (gameObject);
}
How do I get the DestroySelfOnContact script to change the 'wepPresent' variable on the RandomSpawnRandomLocation script ?
Would I be better off having the destroy script attached to my player object as that is always present in the scene ?
???
Answer by _Yash_ · Jan 07, 2015 at 02:17 PM
Sorry i'm not good at javaScript but you can easily find javascript equivalent.
in DestroySelfOnContact script you can
RandomSpawnRandomLocation otherScript;
void Start(){
otherScript = GameObject.find("spawnHandler").GetComponent<RandomSpawnRandomLocation>();
}
void OnTriggerEnter()
{
Destroy (gameObject);
// now you can do otherScript.wepPresent = false/true
}
var wepPresent : boolean = false;
Has now been changed to -
public var wepPresent : boolean = false;
And my destroySelfOnContact script now is -
#pragma strict
public var getSpawnHandler : GameObject;
private var randomWeaponRandomLocation : RandomWeaponRandomLocation;
function Awake ()
{
randomWeaponRandomLocation = getSpawnHandler.GetComponent(RandomWeaponRandomLocation);
}
function OnTriggerEnter()
{
Destroy (gameObject);
randomWeaponRandomLocation.wepPresent = false;
}
I'm getting no compile errors, but nothing is actually happening. Shouldn't this now be affecting/changing the variable on the 'other' script
???
randomWeaponRandomLocation.wepPresent = false;
Destroy (gameObject);
Try destroying after you make ur changes...
Answer by Shrikky23 · Jan 08, 2015 at 02:53 AM
First create an Object to the script, I will quote the other person's reply as a reference.
Modify the above idea, make the var as public .
public var randomWeaponRandomLocation : RandomWeaponRandomLocation;
Check if the gameobject is assigned, if its assigned properly, then make sure you change the value before you destroy the object