- Home /
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);
}
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);
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.
Tried doing that, but nothing gets printed in the console. Does this mean the Awake() isn't even getting initiated?
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.
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.
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
Follow this Question
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