I'm trying to cache a reference to another gameobject script in Start function. It works fine within the start function, but try to access the variable elsewhere and its become null.
using UnityEngine;
using System.Collections;
public class test_cacheComponent : MonoBehaviour {
public GameObject go_AppManager;
private applicationManager m_AppManagerScript;
void Start ()
{
applicationManager m_AppManagerScript = (applicationManager) go_AppManager.GetComponent(typeof(applicationManager));
print("Test>>Start: Load m_AppManager script " + m_AppManagerScript);
m_AppManagerScript.TestApplicationManger();
}
void Update ()
{
print("Test>>Start: Load m_AppManager script " + m_AppManagerScript);
m_AppManagerScript.TestApplicationManger();
}
}
When run this will correctly print and call 'TestApplicationManger' (function simply prints OK) in Start, but come Update and m_AppManagerScript appears to be void. How come? I thought that Start() was called after all objects had been initialised and clearly AppManger is as I can call its functions, but it appears that the reference is only valid in locally in scope to the start function?
In addition I tried to check the variable with this line in Update
if (m_AppManagerScript == null)
{
print("NULL");
applicationManager m_AppManagerScript = (applicationManager) go_AppManager.GetComponent(typeof(applicationManager));
}
But that brings up the same error 3 times in the same script error CS0135: `m_AppManagerScript' conflicts with a declaration in a child block Which i don't understand since i'm not re-declaring the variable at all.
Cheers