Quantcast
Channel: Answers by "ransomink"
Viewing all articles
Browse latest Browse all 80

Answer by ransomink

$
0
0
Are you sure the error is on the line: transform.LookAt(choosenBoulderFish.transform.position); Is that your entire code or just an excerpt? I just tested this out and it worked with no errors. using UnityEngine; using System.Collections; public class RockFish : MonoBehaviour { private bool pickBoulder = true; private GameObject[] nearestBoulderFish; private GameObject choosenBoulderFish; void Update() { DoStuff(); } void DoStuff() { if (pickBoulder) { nearestBoulderFish = GameObject.FindGameObjectsWithTag("BoulderFish"); choosenBoulderFish = nearestBoulderFish[Random.Range(0, nearestBoulderFish.Length)]; pickBoulder = false; } transform.LookAt(choosenBoulderFish.transform.position); } } But it is wiser to create a Manager GameObject inside the scene and create a script that controls the amount of fish (during gameplay), like FishManager. Inside FishManager you can have a list called fish (make it public if you want to see it update inside the inspector). As you did, it is much easier to add each BoulderFish to the list on the FishManager script during their Awake() function since it only happens once. When the BoulderFish is destroyed, you can use a function to remove itself from the list. This is actually a better way of doing it, not a workaround, hope this helps!

Viewing all articles
Browse latest Browse all 80

Trending Articles



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