- Home /
Coud public variables with same names cause problems?
I have pretty weird problem.
First i'll explain what's happening.
I have a character and in the MovementScript there is one function that is killing the player and moving it back to the last CheckPoint. The function simply stops the player from moving, then deactivating only the geometries, not the whole gameObject of the character, so the scripts, can continue working. These deactivations and activations are separated by simple yields with 0.5 to 1.5 seconds.
There are holes in the game and when the player falls in them, he passes through a trigger, that simply calls the function that kills the player. It works just perfectly.
There are also characters, that shoot bullets in the game and when that bullet hits the player, it calls the same function, for killing the player. But when the bullet calls the Kill function it get's executed just untill the first yield and then stops. So the player dissapears and doesn't go back to the checkPoint.
There are no errors, no nothing. It just stops at the yield and happens only when bullet hits player.
The bullet have 2 public variables "speed" and "rotateSpeed". I just found, that if i change the variable of the bullet, from speed, to bulletSpeed, the Kill function is executed untill the end.
So if i have 2 variables on completely different objects, that are public (Not Static), is that a problem???
Answer by legion_44 · Jul 07, 2014 at 10:00 PM
Hmm.. are you calling the kill function with some parameters? Ex. speed (which is then used in yield)? The if you make it just Kill(speed), you're referencing speed from bullet not from the player script. It will be hard to quess without your code ;).
hm. I tried changing the var names, but that doesn't work either.. So what the hell is happening 0_0
This is the code, that is attached to the bullet:
// Public Variables
public var ballSpeed : float = 1.0; // The speed of the ball
public var ballRotateSpeed : float = 1.0; // The speed of rotation
//@HideInInspector
public var flowerDirection : Vector3 = Vector3.up; // Feeded from the flowers, that the ball is in
// Private Variables
private var trans : Transform;
private var ftDirection : Vector3 = Vector3(0, 1, 0); // The direction, that the ball will be flying at
function Awake()
{
trans = transform;
}
function Update()
{
ftDirection = flowerDirection * ballSpeed; // Update the direction, every frame, from the flowerDirection
trans.Translate(ftDirection * Time.deltaTime, Space.World); // Translate the object with deltaTime
trans.Rotate(Random.Range(0, ballRotateSpeed), Random.Range(0, ballRotateSpeed), Random.Range(0, ballRotateSpeed));
}
function OnTriggerEnter (col : Collider)
{
if (col.tag == Tags.player)
{
col.transform.GetComponent(Player$$anonymous$$ovement_JS).BackToCheckPoint(); // If ball hit player, it kills it
}
}
Now. Since the player script is 400 lines of code i will only attach, what's in the BackToCheckPoint() function
function BackToCheckPoint()
{
// Collect information about Character
var rabyGeo : Transform = trans.Find("RabyGeos_grp"); // Get the geo group
var curGravity : float = gravity; // Get the current gravity
var shootScript : PlayerShoot_JS = trans.GetComponent(PlayerShoot_JS); // Get the shootScript
var dyingParticles : ParticleSystem; // The particles that will be instantiated when dying
isAlive = false; // Prevent from moving the player
trans.parent = null; // Set the transform back to worls, in case we were in the elevaator
controller.enabled = false; // Disable character controller
shootScript.shootEnabled = false; // Disable shooting
rabyGeo.gameObject.SetActive(false); // Deactivate geos
gravity = 0; // Set gravity = 0, so we don't fall
vertical$$anonymous$$ove = 0; // Disable horizontal movement
dyingParticles = Instantiate(deadParticle, trans.position, Quaternion.identity);
yield WaitForSeconds(1.5); // Wait some time
trans.position = checkPointPosition + Vector3.up; // $$anonymous$$ove the character to the checkPoint position
yield WaitForSeconds(0.5); // Wait some time
rabyGeo.gameObject.SetActive(true); // Activate the character geos
controller.enabled = true; // Enable characterController
gravity = curGravity; // Restore gravity
isAlive = true; // Enable movement
shootScript.shootEnabled = true; // Enable Shooting
dyingParticles.Clear(); // Clear particle system
dyingParticles.gameObject.SetActive(false); // Disable the particle system
}
Actually i just found, that the BackToCheckPoint(), stops the execution, when the bullet get's destroyed. But what the bullet have to do with the Function, that is attached to the player. It calls it and after that it get's destroyed :\
Aaand just realized, that if one object calls a function, that have yield and get's destroyed immediately, the function stops the execution at the yield.
It also happens if the object, that calls the function get's deactivated, the function also stops at the Yield.
So can somebody tell me how i can make a function that have Yield, continue working after the object, that calls the function get's deactivated...
Answer by MaxLohMusic · Jan 29 at 10:14 PM
I had the same issue. I had two different classes, Player and Enemy, each with their own public variable "health". I found that the enemy somehow was using the player's health. Now I can't speak to whether another bug was a confounding factor; however I can say that after ONLY renaming the player's "health" variable to "playerHealth" using Visual Code refactoring tool doing NOTHING ELSE, everything worked correctly. This does not seem like correct behavior that a program's behavior can change from the way the variables are named!
First of all this question is from 2014 and was about UnityScript (which was a javascript insprired language). So you most likely don't have the "same issue". Please do not necro post ancient questions with an answer that doesn't answer the question. If you have a question, please ask a question and present your actual case. UnityAnswers is not for discussions, that's what the Untiy forums are good for. US is for clear questions that can be answered.
Apart from all that, I highly doubt rena$$anonymous$$g a variable changes the behavior. It's more likely you may had some faulty serialized value which you lost by rena$$anonymous$$g the variable. Have you actually tried to rename the variable back to health? One class can not "somehow" use a variable from a different class. This makes absolutely no sense and is literally impossible unless you specifically access a variable on another object. However in that case there is nothing "strange" going on since you specifically did this. When a PC does "unpredictable" things it can only mean:
You do not fully understand what it actually does or you are missing important information.
it is actually physically broken
There are no other reasons. Note that the first point would includes software bugs as well However those are fully deter$$anonymous$$istic once you understand how it works. If PCs are not reliable they would be useless.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153500im_/https://answers.unity.com/themes/thub/images/avi.jpg)