Search Unity

Question Permanently Instantiate

Discussion in 'Editor & General Support' started by Senseweeks, Jan 17, 2022.

  1. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    Not sure where to put this (Mods please move me if I'm in the wrong place)

    I am having trouble finding a way to make instantiates permanent. My code generates a new object every frame and I need a way to keep all these objects in the hierarchy for later use.

    ie) when the game ends I need the instantiated objects to remain in the hierarchy, and preferably as children to a gameobject but I don't know how to do that

    EDIT: Added My code for referance


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using UnityEngine.XR;
    6. public class TrackAllPositions : MonoBehaviour
    7. {
    8.     public GameObject CenterEyeAnchor;
    9.     public GameObject RightControllerAnchor;
    10.     public GameObject LeftControllerAnchor;
    11.     public Transform TrackerCube;
    12.    
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         // var headPosition = CenterEyeAnchor.transform.position;
    18.         var headX = CenterEyeAnchor.transform.position.x;
    19.         var headY = CenterEyeAnchor.transform.position.y;
    20.         var headZ = CenterEyeAnchor.transform.position.z;
    21.  
    22.         //var handRPosition = RightControllerAnchor.transform.position;
    23.         var handRX = RightControllerAnchor.transform.position.x;
    24.         var handRY = RightControllerAnchor.transform.position.y;
    25.         var handRZ = RightControllerAnchor.transform.position.z;
    26.  
    27.  
    28.  
    29.         //var handLPosition = LeftControllerAnchor.transform.position;
    30.         var handLX = LeftControllerAnchor.transform.position.x;
    31.         var handLY = LeftControllerAnchor.transform.position.y;
    32.         var handLZ = LeftControllerAnchor.transform.position.z;
    33.  
    34.  
    35.         float centroidX1 = (handRX + handLX + headX);
    36.         float centroidX = centroidX1 / 3;
    37.  
    38.         float centroidY1 = (handRY + handLY + headY);
    39.         float centroidY = centroidY1 / 3;
    40.  
    41.        
    42.  
    43.  
    44.     }
    45.    
    46.    
    47.  
    48.     void CreateText()
    49.     {
    50.  
    51.         var headX = CenterEyeAnchor.transform.position.x;
    52.         var headY = CenterEyeAnchor.transform.position.y;
    53.         var headZ = CenterEyeAnchor.transform.position.z;
    54.  
    55.         //var handRPosition = RightControllerAnchor.transform.position;
    56.         var handRX = RightControllerAnchor.transform.position.x;
    57.         var handRY = RightControllerAnchor.transform.position.y;
    58.         var handRZ = RightControllerAnchor.transform.position.z;
    59.  
    60.  
    61.  
    62.         //var handLPosition = LeftControllerAnchor.transform.position;
    63.         var handLX = LeftControllerAnchor.transform.position.x;
    64.         var handLY = LeftControllerAnchor.transform.position.y;
    65.         var handLZ = LeftControllerAnchor.transform.position.z;
    66.  
    67.  
    68.         float centroidX1 = (handRX + handLX + headX);
    69.         float centroidX = centroidX1 / 3;
    70.  
    71.         float centroidY1 = (handRY + handLY + headY);
    72.         float centroidY = centroidY1 / 3;
    73.  
    74.  
    75.         //Path of File
    76.         string path = Application.dataPath + "/Log.txt";
    77.         //Create File if it doesn't exist
    78.         if (!File.Exists(path))
    79.         {
    80.             File.WriteAllText(path, "Centroid Coordinates in XY on");
    81.             File.AppendAllText(path, File.GetCreationTime(path) + "\n\n ");
    82.         }
    83.  
    84.         //Content of file
    85.         string content = centroidX + ", " + centroidY + "\n";
    86.  
    87.         //Add the text
    88.         File.AppendAllText(path, content);
    89.  
    90.      
    91.  
    92.     }
    93.  
    94.     // Update is called once per frame
    95.     void Update()
    96.     {
    97.         // var headPosition = CenterEyeAnchor.transform.position;
    98.         var headX = CenterEyeAnchor.transform.position.x;
    99.         var headY = CenterEyeAnchor.transform.position.y;
    100.         var headZ = CenterEyeAnchor.transform.position.z;
    101.  
    102.         //var handRPosition = RightControllerAnchor.transform.position;
    103.         var handRX = RightControllerAnchor.transform.position.x;
    104.         var handRY = RightControllerAnchor.transform.position.y;
    105.         var handRZ = RightControllerAnchor.transform.position.z;
    106.  
    107.  
    108.  
    109.         //var handLPosition = LeftControllerAnchor.transform.position;
    110.         var handLX = LeftControllerAnchor.transform.position.x;
    111.         var handLY = LeftControllerAnchor.transform.position.y;
    112.         var handLZ = LeftControllerAnchor.transform.position.z;
    113.  
    114.  
    115.         float centroidX1 = (handRX + handLX + headX);
    116.         float centroidX = centroidX1 / 3;
    117.        
    118.         float centroidY1 = (handRY + handLY + headY);
    119.         float centroidY = centroidY1 / 3;
    120.  
    121.         float centroidZ1 = (handRZ + handLZ + headZ);
    122.         float centroidZ = centroidZ1 / 3;
    123.  
    124.  
    125.         //Debug.Log("Centroid Coordinates in XY Plane = (" + centroidX + ", " + centroidY +")")
    126.  
    127.         Instantiate(TrackerCube, new Vector3(centroidX, centroidY, centroidZ), Quaternion.identity);
    128.  
    129.         CreateText();
    130.  
    131.  
    132.      
    133.  
    134.  
    135.  
    136.  
    137.     }
    138.  
    139.  
    140.  
    141.  
    142.  
    143.  
    144.  
    145.  
    146.  
    147. }
    148.  
     
    Last edited: Jan 17, 2022
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    All instantiation is permanent, unless GC is collecting it, but if so then nothing is referencing it anyway so there's no reason for it to exist.
     
  3. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    I do this also, I created my own Prefab controller which creates all objects List<GameObject> I need and simply set IsActive ( or whatever its called now ) to false.

    If I need another I can easily just say MyGameObjects.FirstOrDefault(x => x.PrefabType == "Goblin");

    I would also suggest organizing it by having an empty gameobject as a parent to keep the hierarchy clean.
     
  4. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    What I mean is that after the game ends I need the instantiated prefabs to still be there

    @krupps and @LaneFox it appears I didn't explain myself well sorry
     
  5. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    @Senseweeks
    Yes, copy this script to your assets

    Then public class PrefabController : Singleton<PrefabController>

    This will mark it as DontDestroy

    I have in my project an InitializeScene which has all the controllers I need to always be active and never destroyed (PrefabController, TutorialController, AchivementCOntroller, etc..) all marked as a singleton so I carries over to each scene

    You can use the GUI with the list but I mark it has [HideInInspector] and [NonSerialized}, so Unity won't cache it and I can update the prefabs at runtime

    Let me know if i'm understanding you correctly
     
  6. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11

    So I just add this script to any gameobject and then what? I don't understand how I'm supposed to attach the "property" you've created to my instantiate line. (this is my first unity project so bear with me) Here is my code for reference:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using UnityEngine.XR;
    6. public class TrackAllPositions : MonoBehaviour
    7. {
    8.     public GameObject CenterEyeAnchor;
    9.     public GameObject RightControllerAnchor;
    10.     public GameObject LeftControllerAnchor;
    11.     public Transform TrackerCube;
    12.    
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         // var headPosition = CenterEyeAnchor.transform.position;
    18.         var headX = CenterEyeAnchor.transform.position.x;
    19.         var headY = CenterEyeAnchor.transform.position.y;
    20.         var headZ = CenterEyeAnchor.transform.position.z;
    21.  
    22.         //var handRPosition = RightControllerAnchor.transform.position;
    23.         var handRX = RightControllerAnchor.transform.position.x;
    24.         var handRY = RightControllerAnchor.transform.position.y;
    25.         var handRZ = RightControllerAnchor.transform.position.z;
    26.  
    27.  
    28.  
    29.         //var handLPosition = LeftControllerAnchor.transform.position;
    30.         var handLX = LeftControllerAnchor.transform.position.x;
    31.         var handLY = LeftControllerAnchor.transform.position.y;
    32.         var handLZ = LeftControllerAnchor.transform.position.z;
    33.  
    34.  
    35.         float centroidX1 = (handRX + handLX + headX);
    36.         float centroidX = centroidX1 / 3;
    37.  
    38.         float centroidY1 = (handRY + handLY + headY);
    39.         float centroidY = centroidY1 / 3;
    40.  
    41.        
    42.  
    43.  
    44.     }
    45.    
    46.    
    47.  
    48.     void CreateText()
    49.     {
    50.  
    51.         var headX = CenterEyeAnchor.transform.position.x;
    52.         var headY = CenterEyeAnchor.transform.position.y;
    53.         var headZ = CenterEyeAnchor.transform.position.z;
    54.  
    55.         //var handRPosition = RightControllerAnchor.transform.position;
    56.         var handRX = RightControllerAnchor.transform.position.x;
    57.         var handRY = RightControllerAnchor.transform.position.y;
    58.         var handRZ = RightControllerAnchor.transform.position.z;
    59.  
    60.  
    61.  
    62.         //var handLPosition = LeftControllerAnchor.transform.position;
    63.         var handLX = LeftControllerAnchor.transform.position.x;
    64.         var handLY = LeftControllerAnchor.transform.position.y;
    65.         var handLZ = LeftControllerAnchor.transform.position.z;
    66.  
    67.  
    68.         float centroidX1 = (handRX + handLX + headX);
    69.         float centroidX = centroidX1 / 3;
    70.  
    71.         float centroidY1 = (handRY + handLY + headY);
    72.         float centroidY = centroidY1 / 3;
    73.  
    74.  
    75.         //Path of File
    76.         string path = Application.dataPath + "/Log.txt";
    77.         //Create File if it doesn't exist
    78.         if (!File.Exists(path))
    79.         {
    80.             File.WriteAllText(path, "Centroid Coordinates in XY on");
    81.             File.AppendAllText(path, File.GetCreationTime(path) + "\n\n ");
    82.         }
    83.  
    84.         //Content of file
    85.         string content = centroidX + ", " + centroidY + "\n";
    86.  
    87.         //Add the text
    88.         File.AppendAllText(path, content);
    89.  
    90.      
    91.  
    92.     }
    93.  
    94.     // Update is called once per frame
    95.     void Update()
    96.     {
    97.         // var headPosition = CenterEyeAnchor.transform.position;
    98.         var headX = CenterEyeAnchor.transform.position.x;
    99.         var headY = CenterEyeAnchor.transform.position.y;
    100.         var headZ = CenterEyeAnchor.transform.position.z;
    101.  
    102.         //var handRPosition = RightControllerAnchor.transform.position;
    103.         var handRX = RightControllerAnchor.transform.position.x;
    104.         var handRY = RightControllerAnchor.transform.position.y;
    105.         var handRZ = RightControllerAnchor.transform.position.z;
    106.  
    107.  
    108.  
    109.         //var handLPosition = LeftControllerAnchor.transform.position;
    110.         var handLX = LeftControllerAnchor.transform.position.x;
    111.         var handLY = LeftControllerAnchor.transform.position.y;
    112.         var handLZ = LeftControllerAnchor.transform.position.z;
    113.  
    114.  
    115.         float centroidX1 = (handRX + handLX + headX);
    116.         float centroidX = centroidX1 / 3;
    117.        
    118.         float centroidY1 = (handRY + handLY + headY);
    119.         float centroidY = centroidY1 / 3;
    120.  
    121.         float centroidZ1 = (handRZ + handLZ + headZ);
    122.         float centroidZ = centroidZ1 / 3;
    123.  
    124.  
    125.         //Debug.Log("Centroid Coordinates in XY Plane = (" + centroidX + ", " + centroidY +")")
    126.  
    127.         Instantiate(TrackerCube, new Vector3(centroidX, centroidY, centroidZ), Quaternion.identity);
    128.  
    129.         CreateText();
    130.  
    131.  
    132.      
    133.  
    134.  
    135.  
    136.  
    137.     }
    138.  
    139.  
    140.  
    141.  
    142.  
    143.  
    144.  
    145.  
    146.  
    147. }
    148.  
     
  7. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    I'm not clear what you are trying to do exactly. your definition should be public class TrackAllPositions : Singleton<TrackAllPositions> { ... }

    Also your instantiate does not actually save the object GameObject MyCube = Instantiate(TrackerCube, ... )

    Your code looks wrong, can you tell me what you're trying to accomplish with those vectors. Screenshots or a quick video would be good.

    I think there's a better way
     
    Last edited: Jan 18, 2022
  8. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
  9. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11

    Sorry, I'm a bit late. Basically what I am trying to do is calculate the centroid (which I can do and get the coordinates of) Then I am spawning a tiny sphere (but its still called tracker cube) at the position for visual reference. What I am working towards is getting the program to plot the 3d graph and make a prediction based on "reference" graphs. I have no idea how to work that last part but what I was trying here was to "force" it by leaving all the tiny spheres and using that as a graph for manual comparison. I am also trying to get it to start / stop the entire code when I press the "A" button on the controller but I haven't gotten that to work.

    Example Vid:
     
    Last edited: Jan 19, 2022
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Unfortunately I don't think any of the answers above have to do with what you're attempting.

    The most accurate is LaneFox above where they correctly note that everything is permanent.

    This is totally true until a scene change happens.

    Are you:

    - trying to make objects survive between scene changes?

    - trying to make objects survive between separate runnings of the game?

    - or are you just making a single scene with an iteratively-approaching solution to your graph?

    - something else?

    Each of those things is going to have WILDLY different answers, like not even the same areas of study or systems.

    Your best bet is probably to start with a tutorial in Unity on Youtube for the thing closest to what you want.
     
  11. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11

    Thanks for taking the time to reply! I am trying to make the objects survive separate runnings of the game. I haven't found any material on this on Youtube in the ~5 hours of research I have done so I'm kinda desperate right now
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    As I suspected, 100% of everything answered so far in this thread has NOTHING to do with your actual question.

    It turns out all you're looking for is basic load/save functionality.

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
  13. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    @Kurt-Dekker
    I am doing this for a school project and the goal is to collect the positional data and track it across a distance. I am having trouble graphing the points in 3d so I was hoping to bypass the graph entirely by creating gameobjects centered on the positions then combining them into 1 and comparing them that way. This is my first project in unity so what you are describing makes little sense to me. If you know of a way to actually graph the points and it would be easier to implement than this then please let me know.
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    This sounds like you're trying to engineer it "all at once."

    That will never work.

    You need to iterate one step at a time:

    - gather data
    - transform it for visualisation
    - visualize it somehow
    - etc.

    When iterating, NEVER move forward to the next step until:

    1. the previous step(s) fully work
    2. you fully understand the previous steps

    There are millions of ways to visualize data... you want to work through a few dozen different ways (google!) to understand the basic concept, then work through enough Unity tutorials (again, google!) that you can use Unity to do the basic visualization.
     
  15. Senseweeks

    Senseweeks

    Joined:
    Jan 11, 2022
    Posts:
    11
    @Kurt-Dekker
    I understand that It needs to be done in multiple steps and I'm fine with having multiple programs. I'm just having trouble actually getting the visualization to work. By that I mean I haven't found a way to do it with unity, and id rather keep the project in one program for other reasons. Is there a library I'm missing that can make 3d graphs? The data doesn't need to be plotted instantly as it's generated it can be processed afterward. My due date is this week so I'm trying my best but I'm still lost. Ive even looked in the aset store but there is no 3d graphing aids although there are a ton of 2d graphing aids.
     
    Last edited: Jan 23, 2022
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    There may be libraries to help you do graphing in Unity, I'm not sure.

    Unity has various types of renderers (CanvasRenderer, SpriteRenderer, MeshRenderer, LineRenderer, etc.) and they can be configured with assets (Sprites, Meshes, Textures, Materials, etc.) to produce different visual results.

    It really comes down to iterating towards what you want.