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

Prefabs, Instantiate & Connections

Discussion in 'Scripting' started by KNR, Feb 27, 2015.

  1. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    Hey,

    I am making a 2d vehicle based game for android platform and i have encountered a bit of a problem that i havent figured out yet.

    So what i want to do is have multiple different player gameobjects (bike models), that the user can choose from and instantiate the chosen one and only one.
    The problem is that when i make a player gameobject into a prefab it loses connections to other prefabs.
    For example a player gameobjects connection to ui images(left & right) or a cameras connection to player.

    What would be the suggested and good way to get the image,transform, etc fields found or imported in the inspectors script field.

    Thanks in advance.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I'm not sure what you mean, but are you trying to have prefabs within prefabs? You can't do that in Unity. At least not now, I believe it's on the roadmap for Unity 5. There are plugins on the Asset Store that give you nested prefabs - altough I haven't tried them.
     
  3. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    Hey, what i mean is that i have a player prefab here


    The Right Touch,Left Touch are scirpts that are in the button images inside the UI gameobject not in the Player gameobject it self.
    Also Handle Level Script is on the Game Camera gameobject.
    if i look at the Player gameobject in the prefab folder it has lost all its connections to the other gameobject making the fields null.
    Which makes me wonder how do i instantiate the game object that has null fields on them, how do i get them found.
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    For this, use FindObjectOfType<Player>(); in the Awake or Start function of the respective classes (for example the UI class), and have assessors in the player object so that these classes can get and/or set the info they need.

    For the models, is it an option to have an empty gameObject that hold a list of available bike models, and that when the player chooses, the player object gets a bike prefab set as a child?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. public class Bike
    8. {
    9.     // Id of the bike
    10.     public int id;
    11.     // Name of the bike
    12.     public string name;
    13.     // GameObject that is associated with the bike
    14.     public GameObject bikePrefab;
    15. }
    16.  
    17. [Serializable]
    18. public class BikeCollection : MonoBehaviour
    19. {
    20.     public List<Bike> bikeModels;
    21. }
    22.  
    23. public class Player : MonoBehaviour
    24. {
    25.  
    26.     List<Bike> bikes;
    27.     Bike bike;
    28.  
    29.     void Awake()
    30.     {
    31.         // You will most likely have this somewhere in a UI script or gameManager class
    32.         // for easy access across scenes and only have a reference to a single bike model
    33.         // in the Player class, but this is just an example.
    34.         bikes = FindObjectOfType<BikeCollection>().bikeModels;
    35.     }
    36.  
    37.  
    38.     /// <summary>
    39.     /// Sets the bike choice by id.
    40.     /// </summary>
    41.     /// <param name="bikeId">The bike identifier.</param>
    42.     void SetChoice(int bikeId)
    43.     {
    44.         bike = bikes.FirstOrDefault(i => i.id == bikeId);
    45.     }
    46.  
    47.     /// <summary>
    48.     /// Sets the bike choice by name.
    49.     /// </summary>
    50.     /// <param name="bikeName">Name of the bike.</param>
    51.     void SetChoice(string bikeName)
    52.     {
    53.         bike = bikes.FirstOrDefault(i => i.name == bikeName);
    54.     }
    55.  
    56.     /// <summary>
    57.     /// Instantiates the bike GameObject and sets it as a child
    58.     /// of the player.
    59.     /// </summary>
    60.     void InstantiateAndChild()
    61.     {
    62.         var bikeObj = (GameObject)GameObject.Instantiate(bike.bikePrefab, transform.position, transform.rotation);
    63.         bikeObj.transform.parent = this.transform;
    64.     }
    65. }
    Here is an extremely dirty quick example of what I mean. This way you can have a reference to the available models in a way so that you can choose the bike model in a pre race scene, then set the choice in a game manager, so that it is available to the player object when the scene starts (so set the choice in a singleton manager, instead of in the player like in the example).

    Edit: just saw your new post. The way you have now done it, is a way I would never do it myself (decouple the different bike parts from the player, and create a bike prefab that can be instantiated on the player!!). also don't have the player rely on the camera or input script, but vice versa
     
    Last edited: Feb 27, 2015
    KNR likes this.
  5. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    That is exactly what im looking for, Thanks!
    Now i just need to figure out how to implement it on my project.
     
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Well, if you really can't figure it out, you can always send a PM with your Skype, and I can walk you over the implementation properly :) Just try to figure it out yourself first ;)
     
    KNR likes this.