Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate and move Prefab in an Array of Arrays -- strange behavior

Discussion in 'Prefabs' started by ehabsa, Jun 18, 2019.

  1. ehabsa

    ehabsa

    Joined:
    Mar 15, 2019
    Posts:
    2
    I am trying to put instances of a prefab into an array of arrays to manipulate their movements and I have ran into strange behavior and I am looking for an explanation. I have simplified the code to only one Array to demonstrate what is going on.

    I instantiate a number of copies of the prefab object and add them the array and position them on the screen. But I can't move them. it seems that what is in the array is not the objects on the screen.

    But if I uncomment the line (// sub1 = GameObject.FindGameObjectsWithTag("Target");) It seems that I am recreating the Array with the instantiated objects and I can move them. But I can't do that with an array of arrays.

    I would love an explanation of what is actually happening. am I adding something to the Array but it is not the objects that I am instantiating??
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class MineArray : MonoBehaviour
    8. {
    9.  
    10.     GameObject[] sub1 = new GameObject[10];
    11.  
    12.     [SerializeField] GameObject oneTarget;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.  
    18.         buildObjects();
    19.  
    20.     }
    21.  
    22.     private void buildObjects()
    23.     {
    24.         float zpos = 100;
    25.    
    26.         for (int x = 0; x < 10; x++)
    27.         {
    28.             Vector3 targetPos = new Vector3(-30 , -30, zpos);
    29.  
    30.             Instantiate(oneTarget, targetPos, Quaternion.Euler(0, 0, 0));
    31.  
    32.    
    33.  
    34.  
    35.             oneTarget.transform.position = targetPos;
    36.  
    37.        
    38.                 sub1[x] = oneTarget;
    39.            
    40.     }
    41.  
    42.         void Update()
    43.     {
    44.         moveTargets();
    45.     }
    46.  
    47.     private void moveTargets()
    48.     {
    49.       // sub1 = GameObject.FindGameObjectsWithTag("Target");
    50.         for (int x = 0; x < sub1.Length; x++)
    51.         {
    52.  
    53.  
    54.             Vector3 myPos = sub1[x].transform.position;
    55.  
    56.             Vector3 newPos = sub1[x].transform.position;
    57.  
    58.  
    59.             newPos.x += 5;  // movement speed.
    60.          
    61.  
    62.             myPos = Vector3.Lerp(myPos, newPos, 0.5f * Time.deltaTime);
    63.      
    64.             sub1[x].transform.position = myPos;
    65.  
    66.  
    67.         }
    68.     }
    69. }
    70. [code]
     
    Last edited: Jun 18, 2019
  2. Please use code tags. Your code is unreadable. https://forum.unity.com/threads/using-code-tags-properly.143875/

    Code (CSharp):
    1. var go = Instantiate(oneTarget, targetPos, Quaternion.Euler(0, 0, 0));
    See the exact examples in the manual: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    You're changing the original prefab's position. You should move the instantiated object I think.
    Code (CSharp):
    1. go.transform.position = targetPos;
    and then you can add it to the array:
    Code (CSharp):
    1. sub1[x] = go;
     
    LeonhardP likes this.
  3. ehabsa

    ehabsa

    Joined:
    Mar 15, 2019
    Posts:
    2
    Thank you. That fixes it.