Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Accessing instantiated spline items in C# script?

Discussion in 'Scripting' started by bdharmon, Aug 21, 2023.

  1. bdharmon

    bdharmon

    Joined:
    Aug 18, 2023
    Posts:
    4
    I am using the default circle spline from the Unity Spline package and have placed two prefab gates at opposite corners of the circle spline using the 'Spline Instantiate' component. I am using the instantiate method of 'Instance count'. All other values remain at their default values. How do I access these two instantiated gate objects in my spline container from a C# script? For now, all I am trying to do is log the two gate objects in the console. I have read over the documentation for instantiating everything from the script, however, I want to be able to add items through Unity Editor and access them through the script. I DO NOT want to have to handle adding/settings through the script. I am looking for something along the lines of this:

    NOTE: getInstantiatedItems() is not a real method. I'm hoping that a simple getter method is available for the SplineContainer to retrieve the instantiated items. Screenshot of my editor is attached.

    Code (CSharp):
    1. public class SplineController : MonoBehaviour
    2. {
    3.     public SplineContainer spline;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.        var[] splineInstantiatedItems = spline.getInstantiatedItems();
    9.  
    10.        foreach (var item in splineInstantiatedItems)
    11.        {
    12.             Debug.Log(item);
    13.             // ParkingGateObject
    14.             // ParkingGateObject
    15.        }
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.    
    22.     }
    23. }
     

    Attached Files:

    Last edited: Aug 21, 2023
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
  3. bdharmon

    bdharmon

    Joined:
    Aug 18, 2023
    Posts:
    4
    I've looked through all of that documentation already. It doesn't say how to get the instances of the instantiated items. There also doesn't seem to be a way to set the Count of instantiated items in the script either. You can set the Instantiate Method but no Count.
     
  4. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    50
    Currently there's no public methods for you to access the instantiated items by SplineInstantiate. Every instance may be destroyed on refresh, so there's small reason to store them outside of SplineInstantiate. It's better to bake them to get the real instances.

    If you really want to use them, then, actually the instantiate items are hidden childs right under the SplineInstantiate component, so you can use
    transform.GetChild(index)
    to retreive them.

    Count seems to be
    SplineInstantiate.MinSpacing
    , they use the same field to save spaces. (I looked into SplineInstantiateEditor)
     
    Last edited: Aug 22, 2023
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Personally I keep track(a list) of all my gameObjects(instances). I just make a static list of the class(or parent class) and in the objects(child) class have it's Awake() method add itself to that list. Then I can easily call anything within that class as the reference is easily a static call away(if knowing proper index).

    The list wouldn't have to be static, so you can view it in the inspector, as long as it's public