- Home /
How do I access scripts
Hello I trying to access two scripts . Im try to access the MyClock Script and the ScoreManager Script from the PlayerPreference Script. I've have type in the codes I need in the PlayerPreference script , so I can access ScoreManager . I gotten an error error CS0246: The type or namespace name `scoreManager' could not be found. Are you missing a using directive or an assembly reference? I thought I had code it right . Here is the scripts :
using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine;
public class PlayerPreference : MonoBehaviour { private scoreManager scoreManagerScript; private MyClock MyClockScript;
void Awake ()
{
scoreManager = GetComponent<ScoreManagerScript>();
myClock = GetComponent<MyClockScript>();
}
void Update(){
if ((myClock != NULL) && (scoreManager != NULL) &&
(myClock.m_leftTime >= scoreManager.score))
{
SceneManager.LoadScene("time");
}
}
}
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ScoreManager : MonoBehaviour { public static int score; private Text text;
void Awake ()
{
text = GetComponent <Text> ();
score = 0;
}
void Update ()
{
text.text = "Score :" + score;
}
}
Answer by Highwalker · May 10, 2016 at 06:13 AM
You're trying to access the scripts within the same object. You would have to use FindGameObjectWithTag. Or, you can simply set some public variables and then set them in the editor. So simply change the variables from private to public. And then you'll be able to select the right object in the editor's inspector window.
Answer by tanoshimi · May 10, 2016 at 06:24 AM
private scoreManager scoreManagerScript;
should be:
private scoreManagerScript scoreManager;
that too ^^. Same with the $$anonymous$$yClock variable. Didn't even notice that error.
Your answer
Follow this Question
Related Questions
Can anyone provide some help with that error ? 1 Answer
Can't add script to anything error 1 Answer
Help with script 1 Answer