Hi all,
Sorry for what must be seem to be another ‘newbie’ thread. I’ve followed a plethora of tutorials on the net amongst other resources and I seem to get confused.
In as simple terms as possible and by means of simple ways of doing things too-
- If I declare a variable and want it to take on a pre-existing variables’ value - that resides in another script - what is the easiest and simplest way to call that script/var so I can make it do what I want in my own script? I wrongly assumed I can call a variable by its defined name that exists in another script - but its not as simple as that it seems. 2) If I want to modify the in-built ControllerMotor.js and over-ride some of its functionality - what is the best way to do this? I can see some functions are defined within the script itself whilst others look pre-defined by the engine. Am I best of trying to design my own movement? I want to define some player ‘stats’ that will then change as my ‘game’ progresses?
Thanks in advance
Just to clean up your wording a little bit You can’t “call” a variable (unless it’s a delegate / function pointer). You can “access” variables and read and write them. You can call functions.
Anyway, to access a variable on another component you need a reference to that other script. One of the easiest ways in Unity is that you create a public variable that can hold a reference to this component and assign it in the Inspector.
I guess you use UnityScript?
//UnityScript
var motor : ControllerMotor; // assign the ControllerMotor component in the inspector
function Start()
{
motor.whateverPublicVariableYouWant = something;
}
A pure scripting solution is also possible, but you have to find a way to “find” your desired object
You may have many instances of some object and its associated scripts running at the same time - that’s why you can’t just use the variable name. To solve this, you must have a reference to the object (gameObject, transform, renderer, collider etc.), use reference.GetComponent(ScriptName) to get a reference to the desired script, then access the variable using this reference - something like this:
// player reference: drag the player here at the Inspector, or find
var player: Transform; // it with FindWithTag at Start, for instance
...
// get a reference to player.CharacterMotor:
var chMotor: CharacterMotor = player.GetComponent(CharacterMotor);
// this reference allows access to any of its public variables or functions:
chMotor.movement.maxForwardSpeed = 20; // set maxForwardSpeed to 20
if (chMotor.IsJumping()) print("Player is jumping");
...
CharacterMotor.js is a very complex script, and drives us crazy when we try to modify it. If you need more specific abilities, try to write your own movement script based on the CharacterController.Move example - it’s much simpler and faster, and a good starting point to implement whatever you want with the CharacterController.
Hi and thank you for the help.
@Bunny83 That does make a lot of sense. Your suggestion is to assign a variable to the name of the component in the first place - to save us constantly manually referring back to that script (and saves hassle in future change!). Thereby going further on down my code I can use myvar_for_the_componentname.var1 etc?
I realize it is an example but the reason for using start() ? I’ll read up on it I suppose!
@Aldo Naletto Thank you for that link of the CharacterController.Move that is ideal and will really help me. A pleasure to hear of someone elses dislike of the ControllerMotor.JS ! Your explanation of getting subcomponents was very helpful. Does IsJumping() actually exist in CharacterMotor? I will have to play around!
Thanks again all.