Is there no way to inherit children's components for validation/checking purposes?
Hello everyone. I'm running into a problem with a scenario I've created. I created a bookcase object. And inside of the bookcase are GameObjects that serve as invisible slots (tagged as "slot_1", "slot_2", "slot_3", etc.). I have it set up where books (tagged as "book_1", "book_2," "book_3", etc.) trigger a green light on each respective slot when they are placed correctly.
I'm running into problems with a method that can check or validate whether slots have been activated and the green lights have been turned on for the slots. Is there no way to inherit child object's components across multiple gameObjects within a larger GameObject? I've tried using OnValidate(), GetComponent, GetComponentsInChildren, etc. and I can't seem to access the Color attributes across each child.
Answer by apventures · Sep 03, 2019 at 08:35 PM
For example, here is my script for "bookslot1".
I had to go with creating an individual script per bookslot because I did not know how to verify tags for all slot+book pairing inside of the same script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bookslot1 : MonoBehaviour
{
//change color of LED to green for correct book + slot
public Color corrColor = Color.green;
//change color of LED to red for incorrect book + slot
public Color incorrColor = Color.red;
//change color of LED to null color for blank book + slot
public Color nullColor = Color.grey;
// render material
public Renderer rend;
// starts when collider is entered
void OnTriggerEnter(Collider col)
{
// if book_1 is placed in slot_1, then print true
if (col.gameObject.CompareTag("book_1"))
{
// debug print
Debug.Log("Collision correct; Book 1 is in slot_1");
// the "correct" slot/book color
corrColor.g += 0.1f;
//assign changed color to the material
rend.material.color = corrColor;
}
//
// if any other book is placed in slot_1, then print incorrect
if (col.gameObject.CompareTag("book_2"))
{
Debug.Log("Incorrect Collision; wrong book in slot_1");
// the "incorrect" slot/book color
incorrColor.r += 0.1f;
//assign changed color to the material
rend.material.color = incorrColor;
}
if (col.gameObject.CompareTag("book_3"))
{
Debug.Log("Incorrect Collision; wrong book in slot_1");
// the "incorrect" slot/book color
incorrColor.r += 0.1f;
//assign changed color to the material
rend.material.color = incorrColor;
}
}
// If nothing is placed in slot_1, then blank color material
void OnTriggerExit(Collider other)
{
rend.material.color = nullColor;
}
}