- Home /
Reading Other Scripts stats
Hi. Im trying to make it so that when you increase the Spd variable it changes the max movement speed and im trying to get the variable Spd from my Script Stats. i made sure its spelled right but its not working. heres my coed class CharacterMotorMovement {
// The maximum horizontal speed when moving
@System.NonSerialized
**static var Spd : float = Stats.Spd;**
@System.NonSerialized
var maxForwardSpeed : float = 1;
@System.NonSerialized
var maxSidewaysSpeed : float = 1;
@System.NonSerialized
var maxBackwardsSpeed : float = 1;
// Curve for multiplying speed based on slope (negative = downwards)
var slopeSpeedMultiplier : AnimationCurve = AnimationCurve(Keyframe(-90, 1), Keyframe(0, 1), Keyframe(90, 0));
// How fast does the character change speeds? Higher is faster.
@System.NonSerialized
var maxGroundAcceleration : float = 30.0;
@System.NonSerialized
var maxAirAcceleration : float = 20.0;
// The gravity for the character
var gravity : float = 10.0;
var maxFallSpeed : float = 20.0;
// For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
// Very handy for organization!
// The last collision flags returned from controller.Move
@System.NonSerialized
var collisionFlags : CollisionFlags;
// We will keep track of the character's current velocity,
@System.NonSerialized
var velocity : Vector3;
// This keeps track of our current velocity while we're not grounded
@System.NonSerialized
var frameVelocity : Vector3 = Vector3.zero;
@System.NonSerialized
var hitPoint : Vector3 = Vector3.zero;
@System.NonSerialized
var lastHitPoint : Vector3 = Vector3(Mathf.Infinity, 0, 0);
}
it gives the error : Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js(27,34): BCE0005: Unknown identifier: 'Stats'.
Answer by Lttldude · Apr 17, 2012 at 09:54 PM
Disclaimer: I don't know c# that well.
I think you need to get and define the script before you start accessing it's variables. Is it on the same object, if not then replace gameObject with whichever object it is on.
Should look something like this:
void Start() {
Stats other;
other = gameObject.GetComponent("Stats") as Stats;
}
void Update() {
spd = Stats.spd;
}
Tell me if that works or not. Good Luck.
Josh
Answer by T27M · Apr 17, 2012 at 11:39 PM
void Start() {
Stats statsScript;
statsScript = GetComponent<Stats>();
}
void Update () {
spd = Stats.spd;
}
or
public Stats statsScript;
void Start () {
statsScript = GetComponent("Stats") as Stats;
}
void Update () {
spd = Stats.spd;
}
http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html
Looked at a few in my own scripts aswell, this should work.
Sorry. its in java so i dont know how i would put that in.
What are you using java for? Unity doesn't even compile java.
Answer by Lttldude · Apr 17, 2012 at 11:49 PM
Here is the javascript one:
var statsScript : Stats;
Start() {
statsScript = gameObject.GetComponent("Stats");
}
Update() {
spd = Stats.spd;
}
now it says. i made sure i spelled Stats right. Capital S and then lowercase tats. i dont know why but its not recognizing any scripts. ive tryed having other scripts read Spd and its fine but This one isnt. Btw its the FPS charaqcter motor script. i just modified it. hopefully this helps.
Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js(4,19): BCE0018: The name 'Stats' does not denote a valid type ('not found').
var statsScript : Stats;
its the first part that you gave me.
"Stats" has to be the name of your script. example: The character motor script would be reference as Character$$anonymous$$otor. Just make sure you have a script with that name.