- Home /
Accessing a variable inside a class inside other script...
Hello everybody, i've seen a lot of questions related to that question. I'll ask about access a variable inside a class from other script from other gameObject, such access a script from a enemy for a player, but how I do that? I've tried:
var motor: Character;
var playerDistance : Vector3;
function Start () {
motor = GetComponent(Character);
playerDistance = motor.playerPosition - transform.position;
}
As all of you can see, my playerPosition is a variable inside a class inside my script of the player. I cannot access it, this error appears:
NullReferenceException: Object reference not set to an instance of an object
So I tried this one:
var motor: Character;
var playerDistance : Vector3;
function Start () {
motor = gameObject.GetComponent(Character);
playerDistance = motor.playerPosition - transform.position;
}
I see in the inspector the motor
variable, I've tried to put my player inside it, but when I execute the game, the variable clean itself and throw the error.
I've seen this error in this question: http://answers.unity3d.com/questions/58075/access-variable-in-a-class-from-another-script.html
I've tried it, but didn't work out too.
So you have two scripts,one on player and the other on the enemy I suppose and this is your enemy script
var motor: Character;
var playerDistance : Vector3;
function Start () {
motor = GetComponent(Character);
playerDistance = motor.playerPosition - transform.position;
}
try accessing the 'playerPosition' variable like this.
void Start()
{
var playerpos=GameObject.Find("PlayerGameObjetName").GetComponent("PlayerScript").playerPosition;
playerDistance = playerpos - transform.position;
}
Note - 'playerPosition' variable in player script should not be private and the player class should be $$anonymous$$onobehaviour
No, my player script is more like this
class CharacterInformation {
//the position of the player.
@System.NonSerialized
var playerPosition : Vector3;
}
The playerDistance does not exists on the player script.. I just wanna access the class "CharacterInformation" on the player script and access the var playerPosition which is a Vector3;
Answer by thef1chesser · Oct 28, 2013 at 09:46 AM
In most cases it just means you have to make your variables public. (protected also works in some cases, but than it has more restrictions)
If it still doesn't work add a using [namespace of other class] at the top and it will see the class.
Furthermore it depends on the design pattern you implemented. (or that you just wrote some spaghetti code...) More info on design patterns: http://www.oodesign.com/ With good design implementations you have the correct accessibility for the classes that need it.
Well, i've just wrote some spaghetti code, as you saw, implementations I let for to do after, but the code isn't work out if I cannot access the variable inside my class...Summarizing, my problem is that I don't know access a variable inside a script from other GamObject
step 1:
public var playerPosition : Vector3;
doesn't work? step 2: add:
import [insert namespace of gameObject script]
if ti still doesn't work can you add both scripts completely?
@thef1chesser import
? This is not the nomenclature of C#? Although I'm using JavaScript, but I'll change it later, the import namespace will doesn't work because we are not working with classes that inherit the $$anonymous$$onoBehaviour...
because you use javascript and not C# it's import ins$$anonymous$$d of using.
and import is not the same as inheritage.
Answer by fafase · Oct 30, 2013 at 08:15 AM
I think your problem is that your class does not inherit MonoBehaviour. Then GetComponent cannot work on it. Hence it does not find the component and return null.
class CharacterInformation extends Monobehaviour{}
Your answer
Follow this Question
Related Questions
Access class variables of selected object 1 Answer
Enum style variable 2 Answers
Editting a custom javascript class in the inspector 1 Answer
Access to a variable inside a C# class 2 Answers