Hi,
I stumbled upon scriptable object while trying to set up a demo project and thought it might be worth trying. So, I have a prefab model of a conveyor, there is a main parent called conveyor, and objects that can rotate are grouped as one of its children with the tag “Drive”. There are other stationary objects under the main parent as well. I have attached below script to the main parent:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConveyorController : MonoBehaviour
{
public InputData inputs;
public int ConvIndex = 0;
private GameObject[] objectsToRotate;
private int ConvNo;
// Start is called before the first frame update
void Start()
{
objectsToRotate = GameObject.FindGameObjectsWithTag("Drive");
switch (this.ConvIndex)
{
case 0:
ConvNo = 1;
break;
case 1:
ConvNo = 3;
break;
default:
ConvNo = 0;
break;
}
}
// Update is called once per frame
void Update()
{
if (ConvNo != 0)
{
if (this.inputs.Drives[ConvNo - 1])
{
foreach (GameObject obj in objectsToRotate)
{
obj.transform.Rotate(new Vector3(1, 0, 0), Time.deltaTime * 100);
}
}
if (this.inputs.Drives[ConvNo])
{
foreach (GameObject obj in objectsToRotate)
{
obj.transform.Rotate(new Vector3(-1, 0, 0), Time.deltaTime * 100);
}
}
}
else
{
Debug.Log("Invalid Conveyor Index at ConveyorController.cs");
}
}
}
My code for the scriptable object is as follows:
using UnityEngine;
[CreateAssetMenu(fileName = "InputData", menuName = "ScriptableObjects/InputData")]
public class InputData : ScriptableObject
{
public bool[] Drives = new bool[10]; //Conveyor Drive
}
I was hoping that each prefab instance would access the respective element in Drives[ ] array in the scriptable object and rotate based on that element (as shown below)
However, when I run the code, changing any one element in the Drives[ ] array makes all conveyor instances to run.