Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by junkdog8 · May 11, 2014 at 08:49 AM · javascriptcomponentreferencenull

referencing a component of a gameobject

i have looked and have been unable to find the solution to my current problem or if i found it i didn't understand it. basically i am trying to call up the State of a gameobject called hinge in the hierarchy. instead what i get is "null" what am i doing wrong?

 #pragma strict
     
     var Chest : GameObject;
     private var chestState : Component;
     
     function Start ()
     {
         Chest = GameObject.Find("Hinge");
         chestState = Chest.GetComponent("State");
         print(Chest);
         print(chestState);
     }
     
     function Update ()
     {
         Debug.Log(chestState);
     }
     
     function Raise()
     {
         if(chestState ==  "open")
         {
             animation.Play("Raise");
         }
     }


state refers to a section of script on the other object

 public enum State
         {
             open,
             close,
             inbetween
         }
Comment
Add comment · Show 15
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · May 11, 2014 at 08:54 AM 0
Share

What do you mean with "State"?

avatar image Benproductions1 · May 11, 2014 at 09:31 AM 0
Share

Ins$$anonymous$$d I get is "null"

What is that supposed to mean? Are you getting a NullReferenceException? If so, what line?

@$$anonymous$$ I think it's pretty obvious that State is an enum, as posted in the question.

avatar image junkdog8 · May 11, 2014 at 09:41 AM 0
Share

when i run it the debug comes out with

Null UnityEngine.Debug:Log(Object) Tablet:Update() (at Assets/Tablet.js:16)

avatar image $$anonymous$$ · May 11, 2014 at 10:17 AM 0
Share

I think this will help you:

 chestState = Chest.GetComponent("State") as State;

Oh sorry this is c#.

avatar image junkdog8 · May 11, 2014 at 10:36 AM 0
Share

that got me this

Assets/Tablet.js(16,53): BCE0018: The name 'State' does not denote a valid type ('not found'). Did you mean 'UnityEditorInternal.State'?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 11, 2014 at 02:46 PM

State is an enum, not a class.

You need to get the Class Instance/Script State is part of, that is the component.

Then you can address things on that class instance like any other object.

Additionally, I don't know Unityscript well but it seems to me that State is an Enum definition and not a field, I don't think you can use it the way you are trying to anyway...

Done right, in C# it would look something like this:

File StateHolder.cs:

 public class StateHolder:MonoBehaviour {
      public enum State
         {
            open,
            close,
            inbetween
         }
 
     public State myState = State.open;
 
    
 
     ... etc
 }

File StateGetter.cs

 public class StateGetter:MonoBehaviour {
          
       void Start() {
         Chest = GameObject.Find("Hinge");
         StateHolder chestState = Chest.GetComponent<StateHolder>();
         print(Chest);
         print(chestState.myState);
 
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help Trying to translate cs to js problem with one block of code 2 Answers

How do I fix this Null Reference error and update my score? 2 Answers

Unity get component help 1 Answer

Why is this code getting NullReferenceException 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges