- Home /
the return of : An instance of type is required to access non-static variable ;
Hello everyone
so the question has been asked many times, and i'm not a programers, but I try to learn... anyway: it's been now few hours that i look on the forums, and on the Unity reference websites, to do a simple variable access from a script to another one... and guess what, I didn't succeed, so I tried again and again, and at the end, I kinda think I get it right, BUT I faced this Error found in the title... not completely despaired, it then looked on this forum for an answer, Many before me had the same problems, and awesomely, great people helped them, so I simply tried to understand, copy even the examples, and... I have the same annoying sentence when everyone is usually saying "thx it's working now :)".
It's been 4 hours now that i'm just trying to get a value from a script, it's not even the beginning of the beginning of the most simple thing, and I still don't even know why i'm wrong :)... can someone help me with the code I will copy bellow.... Thx a lot.
Script1.js
private var Get_Script2 : Script2; // I'm just creating a script type container if i'm right.
function Start() {
Get_Script2 = GetComponent(Script2); // Just calling my script with all var in it ?
}
var ActualLength:int = Script2.Length; // (the error is here :) Just calling the value of Length to ActualLength
Script2.js
public var Length : int = 1;
function OnTriggerEnter (ColliderTest : Collider)
{
if (ColliderTest.tag == "TagOnObject")
{
Length++;
}
}
Thx a lot for any help you might bring !!!
Answer by robertbu · Mar 15, 2013 at 06:07 PM
You are close. You need to use the name of your variable not the class/script like this:
var ActualLength:int = Get_Script2.Length;
Answer by krousty_bat · Mar 15, 2013 at 07:07 PM
Thx Robert and Julien.
I would like to point for anyone who wants more information that the link given was really helpful too: http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
Exist in French / Existe en Français aussi http://unitygems.com/interaction-scripts/
Thx again for your help guys !!!
Your answer
Follow this Question
Related Questions
An instance of type "x" is required to access non-static variable "y" 2 Answers
Unserialized private variable values from static instance persist when exiting play mode 1 Answer
Dynamically Changing GameObject makes NullReferenceException(C# Unity) 1 Answer
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers