- Home /
Access Variable in a class from another script
Hi
im using the platform controller that comes with unity and im trying to change the speed of the player within the game. the problem is that the speed var is located with in a class in the Player script and im having trouble accessing it from another script.
its located: Controller then in a class called ControllerMovement then speed.
how do i get to the speed var from another script
Code would be very helpful :) Thanks
EDIT: this is where it is, inside LanganController
#pragma strict
var canControl = true;
var spawnPoint : Transform;
class LangmanControllerMovement { // the class i cant access
var runSpeed = 7.0; // i want to change that Var from another script
and this is how i tried to change it from another script on another object :
// so you assign the player here in the inspector
var target :LangmanController;
function Update() {
var temp = target.GetComponent("LangmanControllerMovment";
temp.gravity = 100;
}
Thanks
Answer by AngryOldMan · Apr 13, 2011 at 10:45 AM
There are two ways around it. Depenging on how and why you want to increase the players speed. If you are using a "pickup" and want the character to speed up when he collects it then add a variable to the part of script that controls speed saying something like
if (HasPickup)
{
Speed = Speed * 1.5;
}
or if you really need to access it from another script make it a public or static variable depending on what method you want to access it with.
Also get component works like this:-
// so you assign the player here in the inspector
var PlayerObject : GameObject;
//get the component from the player, in this case a script
TemporaryVariable = PlayerObject.GetComponent(PlayerScriptWithSpeedOn);
//do something to the speed variable on that script
TemporaryVariable.Speed = TemporaryVariable.Speed * 1.5
i get this error "Object reference not set to an instance of an object"
thats because you just put the code in without modifying it to your needs or you havn't assigned the variable PlayerObject
i changed it to my need but i cant assign PlayerObject any way because my Player isnt a prefab so i changed it to Transform ins$$anonymous$$d and i still get the same error // so you assign the player here in the inspector var target :LangmanController; function Update() { var temp = target.GetComponent("LangmanController$$anonymous$$ovment"); temp.gravity = 100;
} Ps its gravity i want to change not speed
update you post to include this relevant data its no good as a comment. then i'l provide some more help
I updated my question and gave code so should be easier now i hope
Answer by efge · Apr 13, 2011 at 09:25 AM
You could take a look at the reference for GetComponent.
ive tried look at that but because pre new to unity i cant get it to work :( could you give mw the code i would need to access it ?
Take a look at the link in my answer, there are code samples.
Answer by Chris 35 · Apr 14, 2011 at 04:07 AM
ITS ALL GOOD EVERYONE I FIGURED IT OUT I WAS CALLING THE WRONG THING I WAS MEANT TO BE CALLING MOVEMENT.SPEED NOT THE MOVEMENT CLASS . SPEED
I PROBS DONT MAKE ANY SENCE BUT IT DONT MATTER COZ IT WORKS :)
Just want to leave a note on what Chris is talking about, because I was running into the same problem and beat my head over it for way longer than was necessary :P
So in these scripts, you define your class. This class may have a number of default values in it. You then define one or more variables of that class type. What he and I were doing was calling out to the class, and trying to change the variables there. What we should have been doing is finding that variable of that class type, and then changed that. Like this:
class Char$$anonymous$$otor$$anonymous$$ove{ var gravity : float = 10; }
var movement : Char$$anonymous$$otor$$anonymous$$ove = Char$$anonymous$$otor$$anonymous$$ove();
//In other script
getComponent(ScriptName).movement.gravity = whatever;
What we were doing was trying to go getComponent(ScriptName).Char$$anonymous$$otor$$anonymous$$ove.gravity = whatever, which doesn't work at all.
At least, that's what happened to me, and I'm pretty sure we did the same thing :)
Answer by robertmathew · Apr 13, 2011 at 10:53 AM
Let be your first script name called sample
with varibale
var size : float = 10;
Let be your second script name called sample1
GetComponent(sample);
function OnGUI() {
sample.size = 20;
}
No...just no. This has nothing to do with what Chris has asked about, he is accessing a variable on a script, I think I sort of get what you are trying to do here but the whole GUI function is wrong.
Your answer
Follow this Question
Related Questions
Accessing a variable inside a class inside other script... 2 Answers
Declaring & understanding Class in javascript 1 Answer
script needs to derive from monobehaviour 1 Answer
Enum style variable 2 Answers