Im calling Toggle() from another NewScript and would like to know how to specify a Int/gameobject from the list in my NewScript.
This is the code im using as a manager.
public class MyManagerScript : MonoBehaviour
{
public List<GameObject> list = new List<GameObject>();
public void Toggle(int index)
{
if (index < 0 || index >= list.Count)
return;
list[index].SetActive( !list[index].activeSelf );
}
And this is my best guess at how to call the int from the public void Toggle(int index) from my NewScript. I have tried these poor excuses of examples but keep getting errors. Any help would be much welcomed. Thankyou.
public class NewScript: MonoBehaviour
{
int index = 1;
public void SetToggle()
{
Toggle(int =1); //Error
Toggle(index); //Error
}
I would think you are getting an error when calling Toggle because you aren’t including a reference to the script the Toggle function is in. Toggle(index) should work fine once they are properly set up though.
I haven’t done any of this lately, so someone can give the correct code/syntax if I am off… but something like:
in your NewScript:
declare a script reference variable. MyManagerScript scriptRef;
create an Awake/Start function and put following in it:
add in a Find function to get your MMScript… don’t remember the exact command setups.
scriptRef = FindObjectOfType<MyManagerScript>();
then when you call Toggle, you’d need to do:
scriptRef.Toggle(index);
scriptRef being whatever you wanted to call it, of course. It’s been a while, but I think that’s probably all you need to do.
Thankyou Lentaq for your response. I should have stated before hand that i was using only parts of my code to keep the question clean but the funny thing was i never included a way to Find<> the script. I thought that since i specified a public MyManagerScript scriptRef; and used the drag and drop with the component containing MyManagerScript that the rest would sort itself out. Awesome Thankyou.
You are right, I believe. The drag and drop should have negated the need for a Find<>. You were probably simply lacking the call to it in front of your Toggle(index) - the “scriptRef.Toggle(index)” that is.