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 Sartoris · Dec 26, 2013 at 01:48 AM · getcomponent

Having trouble with GetComponent

Here is a code, based on a state machine, that is simply supposed to read one script that contains a simple integer (health = 1) and display it in the console. However, I can't get Unity to execute this code, it keeps saying that villagerScript doesn't exist in the context. I followed the tutorial for GetComponent, but I'm not sure how to fix this.

 using UnityEngine;
 
 using Assets.Code.Interfaces;    
 
 namespace Assets.Code.States
 
 {
 
 public class PlayStateScene1_1 : IStateBase

 {
     public class test : MonoBehaviour
     {
         private Villager villagerScript;
         
         void Awake()
         {
             villagerScript = GetComponent<Villager> ();
         }
     }

     private StateManager manager;
     
     public PlayStateScene1_1 (StateManager managerRef)
     {

         manager = managerRef;
         if(Application.loadedLevelName != "Scene1")
             Application.LoadLevel("Scene1");
     }
     
     public void StateUpdate()
     {

     }

     void Start()
     {
         Debug.Log (villagerScript.health);
             
     }

Comment
Add comment · Show 7
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 Sartoris · Dec 26, 2013 at 03:33 AM 0
Share

I tried doing this:

using UnityEngine;

using Assets.Code.Interfaces;

namespace Assets.Code.States

{ public class PlayStateScene1_1 : $$anonymous$$onoBehaviour, IStateBase

 {
     private Villager villagerScript;

     void Awake()
     {
         villagerScript = GetComponent<Villager> ();
     }

     private State$$anonymous$$anager manager;
     
     public PlayStateScene1_1 (State$$anonymous$$anager managerRef)
     {
         manager = managerRef;
         if(Application.loadedLevelName != "Scene1")
             Application.LoadLevel("Scene1");
     }
     
     public void StateUpdate()
     {
         Debug.Log (villagerScript.health);
     }

     void Start()
     {
     }

As you can see, I had to move $$anonymous$$onobehaviour up next to my interface, IStateBase. However, even in this form Unity won't accept it and throws this error:

NullReferenceException: Object reference not set to an instance of an object Assets.Code.States.PlayStateScene1_1.StateUpdate () (at Assets/Code/States/PlayStateScene1_1.cs:29) State$$anonymous$$anager.Update () (at Assets/Code/Scripts/State$$anonymous$$anager.cs:37)

When I double click the error it points to this line of code:

Debug.Log (villagerScript.health);

avatar image KellyThomas Sartoris · Dec 26, 2013 at 04:01 AM 0
Share

Try changing your Awake() to this:

     void Awake()
     {
        villagerScript = GetComponent<Villager> ();
        if(villagerScript) {
            Debug.Log("villagerScript found");
        }
        else {
            Debug.Log("villagerScript is null");
        }
     }

Then check the console to see if GetComponent() is working.

avatar image Sartoris Sartoris · Dec 26, 2013 at 10:47 AM 0
Share

Tried doing that, but nothing gets printed in the console. Does this mean the Awake() isn't even getting initiated?

avatar image Sartoris Sartoris · Dec 26, 2013 at 10:51 AM 0
Share

I tried adding your code to StateUpdate, and then it prints villagerScript is null. I suppose this means GetComponent isn't working. In that case I really don't know how to make it work. I'm trying to use GetComponent with a simple script called Villager that contains this code:

using UnityEngine; using System.Collections;

public class Villager : $$anonymous$$onoBehaviour { public int health = 100;
}

This script is not attached to any game object.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by KellyThomas · Dec 26, 2013 at 03:05 AM

villagerScript is declared in the scope of your nested type test. Therefore only test can access it.

     public class test : MonoBehaviour
     {
        private Villager villagerScript;
  
        void Awake()
        {
          villagerScript = GetComponent<Villager> ();
        }
     }

If you move it's declaration out to PlayStateScene1_1 you will have access to it from both PlayStateScene1_1 and test.

Comment
Add comment · Show 1 · 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
avatar image Rick.Miranda · Jan 24, 2014 at 12:40 AM 0
Share

I'm trying to solve a GetComponent problem here, any help will be great: http://answers.unity3d.com/questions/623655/help-with-getcomponent.html

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

20 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

Related Questions

NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer

Having trouble understanding parenting with componants 0 Answers

GetComponent is not member of Object? 2 Answers

A simple CS0309 problem... 2 Answers

Using raycast - Why can't I get a variable on the object I hit? 1 Answer


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