- Home /
Calling Method within a Method from a different Class
I hope I got the terminology right!
I'm trying to call a Method that rotates towards a target. The target is being targeted in another script with an array/loop.
This works, like it calls the Method, but I also get a warning:
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
public void Method1Script1()
{
var instance = new Script2();
instance.Method2();
}
I tried this as well:
public void Method1Script1()
{
Script2 script2 = GetComponentInChildren<Script2>();
Debug.Log(script2.Method2());
}
It works just fine outside of Method1().
I could always make the same array/loop for targeting in Script2 but I feel like that would be a bad idea. I don't know if it is? lol running two of the same array/loop to get the same target.
Thanks!
Do you have Script1 and Script2 attached to the same object?
If so then I believe you can use something like this:
Script2 script2 = gameObject.GetComponent<Script2>();
Script1 is on a great grandparent. I was able to call method2() from script1's Start(), just not from method1().
Thanks!