Setting up a New Data Type - Am I Doing Something Wrong?
In my current project, I'm trying to set up a new data type so I can get a script off a game object and enable/disable it on a keystroke. All of my syntax seems to be correct, except for the fact that my new data type isn't being established as I expected it to. Currently I just have things as follows:
private ScriptToToggle script;
public void TinSight()
{
script = GetComponent("ContrastEnhance");
if (useTin == true)
{
Time.timeScale = 0.8f;
}
Time.timeScale = 1.0f;
}
}
I must be missing something here, because with things as they are now I get the "type or namespace name 'ScriptToToggle' could not be found" error. I'm sure I'm missing something obvious, but would someone mind pointing out my mistake?
Do you have a class.struct, enum, etc called ScriptToToggle? I don't quite understand the question
No, I do not. If that's my issue, would you $$anonymous$$d helping me set up whatever would be best for my purposes?
Answer by herDev · Mar 29, 2017 at 07:24 AM
Hi, here's how you can do it if you just want a direct reference...
In this first case you will have a public ScriptToToggle variable in the Inspector and you can just drag and drop the object that contains the script to the slot and create a reference..
public ScriptToToggle script;
public void TinSight()
{
script.methodName();
if (useTin == true)
{
Time.timeScale = 0.8f;
}
Time.timeScale = 1.0f;
}
or alternatively you can do it like this
public GameObject scriptToToggleObj; // same as above really, you'll need to drag and drop the object that contains the script to the inspector variable slot and create a reference
ScriptToToggle script;
public void Start()
{
script = scriptToToggleObj.GetComponent<ScriptToToggle>();
}
public void TinSight()
{
script.methodName(); // execute whatever method you want to run from the script
if (useTin == true)
{
Time.timeScale = 0.8f;
}
Time.timeScale = 1.0f;
}
I feel like that should work, but Unity still isn't recognizing ScriptToToggle as a new type or namespace. It tells me the same thing it did, and that Component doesn't include a definition for 'enabled'.
Your answer
Follow this Question
Related Questions
How can I have a life added bar every time I reach 5000 points 0 Answers
Unity2D Door and Button Pairing,Unity2D door and button pairing 0 Answers
Input Command Issues 0 Answers
What is Coroutine, Yiel and IENumerator? 1 Answer
Can't seem to find a proper stepping stone to learn from, any ideas? 3 Answers