- Home /
Building resets variables
Hi guys! I have a little question. I'm currently doing the Walker Boys tutorial and I've encountered a little problem.
I have cubes and spheres going around the screen. in a script common for both, I have a Boolean variable that tells if the object is a sphere (true) or a cube (false), the default is false. I then selected my spheres and set the variable to true in the inspector. But when I build the project, and try it, the variable seems to have been reset to the script default, because I see the spheres not doing what they're supposed to do.
// Enemy Script
// Inspector variables
var numberOfClicks : int = 2;// Hitpoints of objects
var waitTime : float = 2.0; // time to wait before respawn
var shapeColor: Color[]; // Color of Object
var explosion :Transform; // load particle effect
var enemyPoints : int = 1;//Value of enemy
var isSphere : boolean = false;
var alive : boolean;
var otherScript;
// Private variables
private var storeClicks : int;var sphere : boolean = false;
// Start is called only once.
function Start(){
storeClicks = numberOfClicks;
var startPosition = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0); //new random position for GameObject
transform.position = startPosition; // relocate GameObject to new location
RespawnWaitTime();
otherScript = GetComponent(scriptMoveSphere);
}
// Update called every frame
function Update () {
if(numberOfClicks <= 0){
if (explosion){
Instantiate(explosion, transform.position, transform.rotation); // Create explosion
}
RespawnWaitTime();
if (isSphere){
otherScript.initialPosition = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0); //new random position for GameObject
}
else{
var position = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0); //new random position for GameObject
}
transform.position = position; // relocate GameObject to new location
numberOfClicks = storeClicks;
}
}
// RespawnWaitTime is used to hide the GameObject and show it again after a set amount of time
function RespawnWaitTime(){
renderer.enabled = false;
alive = false;
if (isSphere)
waitTime = Random.Range(1,3);
yield WaitForSeconds(waitTime);
RandomColor();
renderer.enabled = true;
alive = true;
}
//RandomColor is used to change the color of the GameObject
function RandomColor(){
if (shapeColor.length >0){
var newColor = Random.Range(0,shapeColor.length);
renderer.material.color = shapeColor[newColor];
}
}
I cannot use different tags because they're already use for a check in a raycast hit.
Has anybody an idea of how I could solve this problem?
I've added the part of the script. This here is inside the function update(). isSphere is the Boolean, if true, it means it's a sphere... I guess what would help is another way to define the sphere, like a function that would find an attribute that is unique to the sphere and return true if it finds it (could we recognize the game object as the sphere? or maybe recognize the name of the gameObject?),
$$anonymous$$ind of hard to help if you can't use tags. That's tough. If you post your whole script then I can know what to work with. Its hard when only seeing a sliver of it.
alright the whole script is here, I use the the boolean isSphere 2 times in this, once in the update() function and the second is in WaitTimeRespawn() function
The script is used as a kind of spawner, but ins$$anonymous$$d of destroying the object and creating it again, it hides it and reveal it again later at another location... personally, I don't like it, I would rather have a empty gameObject serving as a spawner that this, but this is what I have to work with at the moment.
Answer by FTheCloud · Aug 05, 2011 at 09:48 AM
Ok, new answer.
Add this to your start function and it should be fine.
function Start(){
if(collider.GetType() == SphereCollider){
isSphere = true
}
storeClicks = numberOfClicks;
var startPosition = Vector3 (Random.Range(-5,5),Random.Range(-3,3),0);
transform.position = startPosition; // relocate GameObject to new location
RespawnWaitTime();
otherScript = GetComponent(scriptMoveSphere);
}
This will tell the script when it starts whether its attached to a sphere or not.
Your answer
Follow this Question
Related Questions
Inspector defined variable wiped away when game is built. 2 Answers
Distribute terrain in zones 3 Answers
Game works fine in editor, but build fails with 59 error in the console 2 Answers
Variables not showing up in inspector 1 Answer
The box for entering a variable's value in the inspector is not where it should be. 0 Answers