Question about ScriptableObject

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.

Welcome to debugging! Here is how to get started on your exciting new debugging adventure:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

1 Like

Oh yes, Debug.Log() is indeed my favourite function in Unity. I had to deliberately remove my debug statements from the code before posting. :smile:

I have found that the code is being executed, because I included a Debug.Log as below:

    void Update()
    {

        //Debug.Log("Conveyor Index: "+ this.ConvIndex + "Conveyor Name: " + ConvName);
        if (ConvNo != 0)
        {
            if (this.inputs.Drives[ConvNo - 1])
            {
                Debug.Log("Conveyor Index: " + this.ConvIndex + " Conveyor Name: " + ConvName + "ConvNo: " + ConvNo);
                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");
        }
    }
}

I can see from the Console,
8861544--1209147--upload_2023-3-8_16-5-30.png

So I am sure the code is executing correctly, but what I do not know is why all the conveyors are responding. My first guess was I did not specify the index correctly, but the below shows the index for first conveyor,
8861544--1209159--upload_2023-3-8_16-7-55.png

and this is my second conveyor:
8861544--1209156--upload_2023-3-8_16-7-37.png

So that was also ruled out. I am now doubting if assigning the tag Drive to child is causing the issue, as the tag is shared by all prefabs, but then,
if (this.inputs.Drives[ConvNo - 1]) should be able to prevent this from happening. ChatGPT told me that I need to create multiple instances of the scriptable object, but the scriptable object is populated by a TCP write stream and I can’t dynamically create the objects. So I thought I might give it a go in the forum.

(I am an absolute beginner at Unity:()

8861544--1209153--upload_2023-3-8_16-6-41.png

objectsToRotate = GameObject.FindGameObjectsWithTag("Drive");

FindGameObjectsWithTag searches the entire scene not just the children of the object you call it on. So every instance of your ConveyorController is grabbing every object tagged “Drive” and moving them all.

2 Likes

Yep, this is it. I didn’t know the taggs would be searched from the entire scene. Thank you. Although now I have got an ugly looking code,

Transform firstChildTransform = this.transform.GetChild(0); // get the first child transform
        Transform firstGrandchildTransform = firstChildTransform.GetChild(0); // get the first grandchild transform

        //GameObject childObject = this.transform.Find("RotaryObject").gameObject;
        Debug.Log("Number of Children: " + firstGrandchildTransform.transform.childCount);

        objectsToRotate = new GameObject[firstGrandchildTransform.transform.childCount];
        for (int i = 0; i < firstGrandchildTransform.transform.childCount; i++)
        {
            objectsToRotate[i] = firstGrandchildTransform.transform.GetChild(i).gameObject;
        }

Thanks again