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

Bug Problem with instantiated gameobject.

Discussion in 'Editor & General Support' started by gwnrobin, Aug 31, 2023.

  1. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    I am getting different behaviour from a prefab that is placed in the world and a prefab that is instantiated. When I put my prefab in the world everthing works fine. but when I Instantiate the prefab it seems like the code is not able to track a transform that is on a other child then where the code is on.

    this is the prefab that is placed in the world like how it should work. here you see the red and a couple of green pixels from the lines that are in the same place.

    https://gyazo.com/8f3cecead50dd7b968a57bb76260dc0c

    now I Instantiate the same prefab from before.

    in the gif you can see the lines do not allign with eachother and I do not understand why.
    the code and the prefabs are exactly the same.

    I hope someone can help me with this I am really stuck.
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    How does your prefab find the transform that's on the other object?
     
  3. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    It finds with a serializefield in the inspecter. and it is within the same prefab. so the other object is just a child gameobject. maybe I explained it weird but it is all within the same prefab
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    When you instantiate the prefab is it still assigned in the instance?
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,833
    Is this child object you're referencing INSIDE the prefab hierarchy? Or is it some child of some other object you were hoping to track?

    References to other objects OUTSIDE the prefab are not saved IN the prefab. Those become null/empty/none. When you Instantiate the prefab, you must then assign any fields to objects in your scene as appropriate.
     
  6. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    yes it is
     
  7. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    it is referenced within the prefab. I do not get a null reference error.

    the prefab that I put in the and the prefab that I Instantiate are exactly the same but the transform does something weird.

    and this code
    "Debug.DrawRay(_itemUseTransform.position, _itemUseTransform.forward, Color.green);"
    I run in the update. does different things like I showed in the image and gif depending on if I instantiate or just put the prefab in the world
     
  8. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Post your code! Don’t force us to guess potential issues when it may simply be a bug in your code.
     
  9. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    Code (CSharp):
    1.     [SerializeField]
    2.     protected Transform _itemUseTransform;
    3.     protected virtual void Update()
    4.     {
    5.         Debug.DrawRay(_itemUseTransform.position, _itemUseTransform.forward, Color.green);
    6.     }
    it is just this and to spawn the prefab I do this


    Code (CSharp):
    1.     public GameObject spawn;
    2.     void Start()
    3.     {
    4.         Instantiate(spawn);
    5.     }
     
  10. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    So I played around some more and it seems it is in the order of code. because when I put the DrawRay inside a LateUpdate it seems to work like it should. but it is still very weird I dont have this problem at all when I just put the prefab in the world instead of instantiating it. it is like when the prefab gets instantiated the order of code is just a little different.

    I fixed it by using a IEnumerator and use WaitEndOfFrame but it seems like a really scuffed solution.

    I will try to better explain the problem in my next answer because I understand it better now aswel
     
    Last edited: Sep 1, 2023
  11. gwnrobin

    gwnrobin

    Joined:
    Jan 4, 2021
    Posts:
    9
    Here is the code
    Code (CSharp):
    1.         private void Update()
    2.         {
    3.             Debug.DrawRay(transform.position, transform.forward, Color.red);
    4.         }
    5.  
    6.         private void LateUpdate()
    7.         {
    8.             Debug.DrawRay(transform.position, transform.forward, Color.yellow);
    9.         }
    I thought I understood is but apparently I dont. because now it doesnt seem to matter if I instantiate the prefab or place it in the world.

    these 2 line should be in the same place

    upload_2023-9-1_11-34-42.png

    but the problem is probably happening because of a lot of other code
     

    Attached Files: