Passing variables from one script to all clones
I am starting the Create with Code series of tutorials, week 2 (yes, I am a noob). I am trying to extend the behaviour of the game by making a system in which the animals ask for an specific type of food, and the player has to cycle between two available types of food ammo. I created a script "Food Tought" that adds a bubble next to each animal to show what they want to eat. I am trying to pass the index number of this script (which picks one of the food options at random) to my "Detect Collisions" script, so the food will only add points to the player and will destroy the animal object only if the correct ammo collides with the Animal. I am stuck. Everytime the script runs it says the console says that the "Object reference not set to an instance of an object", and the variable doesn't go through. I believe that the problem lies the clones of my animals, because I have a script that is spawning clones at random, and my guess is that the variable is not passing to the clones but only to the original object. I'll leave both my "Food Thought" and "Detect Collisions" scripts here. Thanks in advance.
Food Thought:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FoodThought : MonoBehaviour
{
public GameObject[] animals;
private DetectCollisions detectCollisions;
public GameObject foodThought;
private int foodIndex;
// Start is called before the first frame update
void Start()
{
foodIndex = Random.Range(0, 2);
changeFood();
animals = GameObject.FindGameObjectsWithTag("Animal");
animals[0].GetComponent<DetectCollisions>();
animals[1].GetComponent<DetectCollisions>();
animals[2].GetComponent<DetectCollisions>();
}
// Update is called once per frame
void Update()
{
}
public void changeFood()
{
if (foodIndex == 0)
{
foodThought.transform.GetChild(0).gameObject.SetActive(true);
foodThought.transform.GetChild(1).gameObject.SetActive(false);
detectCollisions.UpdateIndex(foodIndex);
}
else if (foodIndex == 1)
{
foodThought.transform.GetChild(0).gameObject.SetActive(false);
foodThought.transform.GetChild(1).gameObject.SetActive(true);
detectCollisions.UpdateIndex(foodIndex);
}
}
}
Detect Collisions:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectCollisions : MonoBehaviour
{
private GameManager gameManager;
private int foodType;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{
}
public void UpdateIndex(int foodIndex)
{
foodType = foodIndex;
if (foodType == 0) { Debug.Log("foodType:" + foodType); }
if (foodType == 1) { Debug.Log("foodType:" + foodType); }
}
void OnTriggerEnter(Collider other)
{
//Check if the other tag was the Player, if it was remove a life
if (other.CompareTag("Player"))
{
gameManager.AddLives(-1);
Destroy(gameObject);
}
//Check if the other tag was an Animal, if so add points to the score
else if (other.CompareTag("Animal") && other.CompareTag("pizza") && foodType == 0)
{
other.GetComponent<AnimalHunger>().FeedAnimal(1);
Destroy(gameObject);
}
else if (other.CompareTag("Animal") && other.CompareTag("steak") && foodType == 1)
{
other.GetComponent<AnimalHunger>().FeedAnimal(1);
Destroy(gameObject);
}
}
}
Answer by PeKenaLarVa · Jul 30, 2020 at 01:21 AM
BTW, if anybody wants to peek on the project, here it is.
Your answer
Follow this Question
Related Questions
Passing variables from one script to all clones 0 Answers
Edit variable in instantiated clone 1 Answer
How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers
Change a variable in another script when collider on trigger enter c# 2 Answers
Add GameObject to List by tag not working consistently 1 Answer