- Home /
How to activate a function on script B but its called from A
So basically I have a script called "NumberDecider" that is on a parent object and I want that script to call a function from "DownArrowScript" (that is a child object) that animates an arrow that but for it to work or "activate" on the "DownArrowScript" itself. For example I want the "NumberDecider" to be like the main guy and give commands on when an arrow script should activate. So to simulate this I just made an int so that it can always be 1 so the function will always play anddd when I call it, it either plays the animation itself on the "NumberDecider" object and not the arrow object I'm trying to animate...or it gives me an error: "NullReferenceException: Object reference not set to an instance of an object DownArrowScript+d__5.MoveNext () (at Assets/Scripts/ArrowKeys/DownArrowScript.cs:31)"
And the error is pointing to the .play animation line...I think what its trying to tell me is that it wants to animate on the parent object but I don't want that. Here is the code and I just literally want a way to play an animation when another script gives an "okay" to. Thank you
Number Decider Script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NumberDecider : MonoBehaviour { [System.NonSerialized] public int rand1, rand2, rand3, rand4; [System.NonSerialized] public int Down1Counter = 0, Up2Counter = 0, Left3Counter = 0, Right4Counter = 0;
public DownArrowScript downArrowScript;
private int Num = 1;
void Start()
{
//how many time the animaitons plays cause it counts it...
rand1 = Random.Range(1, 5);
Debug.Log(rand1);
rand2 = Random.Range(1, 5);
Debug.Log(rand2);
rand3 = Random.Range(1, 5);
Debug.Log(rand3);
rand4 = Random.Range(1, 5);
Debug.Log(rand4);
//now it acutally counts the arrows in a counter
//for down arrow
if (rand1 == 1)
{
Down1Counter = Down1Counter + 1;
}
if (rand2 == 1)
{
Down1Counter = Down1Counter + 1;
}
if (rand3 == 1)
{
Down1Counter = Down1Counter + 1;
}
if (rand4 == 1)
{
Down1Counter = Down1Counter + 1;
}
//up arrow
if (rand1 == 2)
{
Up2Counter = Up2Counter + 1;
}
if (rand2 == 2)
{
Up2Counter = Up2Counter + 1;
}
if (rand3 == 2)
{
Up2Counter = Up2Counter + 1;
}
if (rand4 == 2)
{
Up2Counter = Up2Counter + 1;
}
//left arorw
if (rand1 == 3)
{
Left3Counter = Left3Counter + 1;
}
if (rand2 == 3)
{
Left3Counter = Left3Counter + 1;
}
if (rand3 == 3)
{
Left3Counter = Left3Counter + 1;
}
if (rand4 == 3)
{
Left3Counter = Left3Counter + 1;
}
//right arrow
if (rand1 == 4)
{
Right4Counter = Right4Counter + 1;
}
if (rand2 == 4)
{
Right4Counter = Right4Counter + 1;
}
if (rand3 == 4)
{
Right4Counter = Right4Counter + 1;
}
if (rand4 == 4)
{
Right4Counter = Right4Counter + 1;
}
if (Num == 1)
{
//downArrowScript.PlayTheDownAnimation(Down1Counter, 1F);
StartCoroutine(downArrowScript.PlayTheDownAnimation(Down1Counter, 1F)); *HERE IS WHERE I CALL THE COROUTINE TO ACTIVATE ON THE OTHER OBJECT*
}
}
void Update()
{
}
}
Down Arrow Script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DownArrowScript : MonoBehaviour { Animator animatorDown;
public NumberDecider numberDecider;
private int HowManyTimesToPlayTheUpAnimation;
//1 is down 2 is up 3 is left and 4 is right
void Start()
{
animatorDown = GetComponent<Animator>();
}
private void Update()
{
}
public IEnumerator PlayTheDownAnimation(int n, float time)
{
Debug.Log("The function call went through");
while (n > 0)
{
animatorDown.Play("DownArrowAnimation", -1, 0F); *HERE IT GOES THROUGH BECAUSE THE DEBUG LOG SAYS SO...BUT IT GIVES THE ERROR I MENTIONED EARLIER*
--n;
yield return new WaitForSeconds(time);
}
}
}
SOME PICTURES TO HELP..I EVEN ATTACHED THEM TO EACH OTHER IN ORDER TO ACESS THEIR VARIABLES AND STUFF...THE FUNCTION WORKS ITS JUST SOMETHING WITH ANIMATION...like I said I just want a way for one script call to activate on another script function
Answer by tuinal · Jul 02, 2021 at 02:31 AM
You don't call the coroutine to start on the other object, you call it to start on NumberDecider. StartCoroutine starts the coroutine on the object the script is attached to. It is not the same as a method call.
Try instead making a public method in NumberDecider that contains the StartCoroutine call, and calling that method from DownArrowScript.
@tuinal I will give this a try, thank you very much for responding! Also just a comment, I did put the coroutine call in Start function on the NumberDecider, but I will give it a try to getting more info on these "methods" your talking about, I don't know much program$$anonymous$$g lingo.
if (Num == 1) { StartCoroutine(downArrowScript.PlayTheDownAnimation(Down1Counter, 1F)); HERE IS WHERE I CALL THE COROUTINE TO ACTIVATE ON THE OTHER OBJECT
}
Your answer
Follow this Question
Related Questions
My animation is not playing 2 Answers
How to Destroy the DontDestroyOnLoad() 2 Answers
How do I make a photography function? 0 Answers