- Home /
 
Object Reference Required To Access A Non-Static Field
I Have Been Searching All Day, And Can Not Find Out How To Use A Object Reference. I Have Tried
public RoomGener Test = GameObject.Find("Script Holder").GetComponent();
And It Still Says I Cant Access A Non-Static Field Without An Object Reference.
Answer by hamstar · Sep 29, 2013 at 06:19 PM
Assuming "RoomGener" is your component (script) name and "Script Holder" is your GameObject name, you need to pass "RoomGener" as the component Type when you call GetComponent.
JS:
 public RoomGener Test = GameObject.Find("Script Holder").GetComponent(RoomGener);
 
               C#:
 public RoomGener Test = GameObject.Find("Script Holder").GetComponent<RoomGener>();
 
               [edit] You would normally initialize your variables in Start, after the game objects have been instantiated.
 public RoomGener Test;
 
 Start() {
   Test = GameObject.Find("Script Holder").GetComponent(RoomGener);
 }
 
              Well RoomGener Is The Script, Witch Is Attached To An Object Named Script Holder, And I Tried This
 void Start() {
         public RoomGener Test = GameObject.Find("Script Holder").GetComponent<RoomGener>();
 }
 
                  And It Said Unexpected Symbol "Public"
You need to declare it as I did, outside of any method declarations. It will then be accessible/useable in all methods in the script.
  public class ScriptName : $$anonymous$$onoBehaviour {
       public RoomGener test;
       void Start() {
         test = GameObject...
 
                  etc
Hello I am having the same issue and it appears here for rigidbody2d.addforce ![alt text][1]
could you please try to help me understand this error i'm also doing the leg work and searching multiple sources and asking around but no luck. [1]: /storage/temp/84416-screenshot-617.jpg
Your answer
 
             Follow this Question
Related Questions
Getting Variables From An Arbitrary Component? 0 Answers
"transform" in a static function 1 Answer
Get component in parent 2 Answers
GetComponent 1 Answer
Why wont this work? (I've done similar 1000 times fine) 1 Answer