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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefabs and Instantiate

Discussion in 'Scripting' started by temp17, Aug 18, 2015.

  1. temp17

    temp17

    Joined:
    Sep 9, 2013
    Posts:
    7
    Hi.
    I want to instantiate a prefab and save the modified new prefab object as an atribute of another Gameobject to access it later.
    But if I access i get only the value of the origin prefab and dont the changed values.
    Why is it so?

    Her some code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4.  
    5. public class LevelDungeon1 : MonoBehaviour {
    6.  
    7.     public GameObject prefab;
    8.     private GameObject childObject ;
    9.     private List<GameObject> list;
    10.  
    11.     void Start () {
    12.         list= new List<GameObject>();
    13.  
    14.         //start
    15.      
    16.         //change prefab attributes with methods
    17.         //..... prefab.GetComponent<Script1>().method1();
    18.         //..... prefab.GetComponent<Script1>().method2();
    19.  
    20.         childObject = Instantiate(prefab) as GameObject;
    21.         list.Add(prefab); //Here I want to save the modified prefab in a list. so i can later access the modfied values.
    22.         childObject.transform.parent = this.gameObject.transform;
    23.  
    24.     void Update () {
    25.    
    26.     }
    27. }

    In the script of the prefab i change for example:
    Code (csharp):
    1.  
    2. void Start() {
    3.     this.name="newName";
    4. }
    5.  
    but if i check the value it is the origin prefab name, originName ..
     
    Last edited: Aug 18, 2015
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    I guess you're mixing up prefabs and instances.
    Instantiate creates a new gameobject (the instance) from the prefab (the template). It does not change the prefab itself, nor it does create a new prefab.
     
  3. temp17

    temp17

    Joined:
    Sep 9, 2013
    Posts:
    7
    ok! I created a level complete level with this script above. The level parts are in another script. There i save start and exit gameobject. the Vector2 is important for me i want to access later. The values are right i see it at runtime in the inspector but now i want to access these attributes (start/exit) of the levelpart. but if i want Gameobject.Find("part1").... there comes NULL. in the Start method. i want to access the level part what i see in the inspector there are the correct values.
    What is my mistake?

    My idea:
    -create a whole level in one script
    -create levelparts with another script
    -access start and exit to connect the parts like a puzzle :)

    and I cant access the Gameobject start, exit i got the prefab values or the last created one.

    How is it possible to create an instance of an prefab an save the created gamobject reference in an attribute?
     
    Last edited: Aug 19, 2015
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. // the name on the prefab template, doesn't change as the script attached to it isn't
    3. //"running" because there isn't a gameobject for the script component to be attached to
    4. prefab.name;
    5.  
    6. // the name of the instance created by using the prefab and instantiate
    7. // name changes once Start has run
    8. childObject.name;
    9.  
    10.