Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

How to activate a function on script B but its called from A

$
0
0
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(); } 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* ![alt text][1] ![alt text][2] [1]: /storage/temp/182895-unity-picture-1.jpg [2]: /storage/temp/182896-unity-picture-2.jpg

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>