Search Unity

Can I Spawn Object overtime, 1 child at a time.

Discussion in 'Scripting' started by GingerNinja04, Jul 23, 2014.

  1. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    Hi guys,

    Basic question, i have tried searching for answers and i'm not sure if i'm even searching with the correct words.

    What i'm trying to do is write a script That will spawn an object (Example a Log Wall) as you place materials into it. So I understand how to go about the Interaction part of the script but before I start writing it i'm trying to work out how/Can I i spawn an object piece by piece.

    Say the object i want to build is a vertical stack of logs.
    My 3d model has each log as a separate Object.
    When I place my Object into the scene, In the Hierarchy it shows each individual Log as a Child.

    My expectations are, as the player adds a log to the Object, the transparency script on LogX will be removed showing it as fully visible and a collider mesh is activated.

    So...
    Can I do what i'm hoping to do?
    i'm only asking because I havent found a way to interact with child objects yet.
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    The answer will always be yes. You can program anything with time and skill.

    As for spawning what's the issue? you don't know how to create things on the fly? Look into instantiate

    or if you want to do it the way you suggested where you already have the object and you want to turn the renderer off and on etc then that would be pretty simple.

    You could have a array of your game objects, or even just the renderers (what ever you're comfortable with) and then toggle the current one on and all the one's below etc. If you're a newbie this might seem alien to you so give me a min and i'll whip up an example
     
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Ok here's a quick example, I don't turn the renderer off, I just set the game object to active or not. If you want to do it using the renderer then you'll be able to change it easily.

    This is in C#, if you're using JS you'll want to change it slightly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LogToggle : MonoBehaviour {
    5.  
    6.     public GameObject[] LogObjects; //The array of log objects
    7.     public int logNumber = 0; //The number of logs we want to toggle, change this based on your input etc
    8.  
    9.     void Update () {
    10.         logNumber = Mathf.Clamp(logNumber, 0, LogObjects.Length - 1);
    11.  
    12.         //Lets loop through our LogObjects array, if the logObjects index is less than our logNumber then set it to active, otherwise lets hide it.
    13.         for(int i = 0; i < LogObjects.Length; i++) {
    14.             if(i < logNumber)
    15.                 LogObjects[i].SetActive(true);
    16.             else
    17.                 LogObjects[i].SetActive(false);
    18.         }
    19.     }
    20. }
    21.  
     
  4. GingerNinja04

    GingerNinja04

    Joined:
    Jul 10, 2014
    Posts:
    16
    thanks for the fast reply @smitchell .
    I am using C#.
    And I know I can change the shader by script but what i need is how to access the Child from the Parent Item.
    LargeLogwall (Parent)
    >VertLog1(child 1)
    >Vertlog2(child2)
    >VertLog3 (child 3)
    >VertLog4 (child4)
    >HorzLog1(child 5)
    Ect ect...

    So if i was to use your script above....
    Code (CSharp):
    1.  
    2. public GameObject[] LogObjects; //The array of log objects
    3. public int logNumber = 0;
    4.  
    GameObject can be = to LargeLogwall but that would be every child object.
    since posting this, i did find some information about GameObject.GetComponentInChildren. but if i was to use that i would need to place my shader/transparency script on each child. and then use GetComponentInChildren to access each script.

    The desired effect I'm after is that similar to "The Forest" where the player places down a "PlaceHolder" of the object they are going to build and ass materials are added the object shows those materials in Realtime.

    Sorry if I seem like an extreme noob, but i am. I have only just begun learning and playing with C#. but, i'm 23 and i llove the feeling when i can sit back and say... I made that. and this is how it does what it does... :S

    Regards,
    Jamie
     
  5. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    maybe you're over thinking things. I'm not quite sure if I really understand you but if all you're trying to do is place a placeholder mesh. then all you need to do is change the material. if it's in preview mode right?

    I'd do something like this

    Code (CSharp):
    1.  
    2.    public Material normalMat;
    3.    public Material previewMat;
    4.  
    5.     public bool isInPreviewMode;
    6.  
    7.     void Update () {
    8.         //Set your preview based on Input. For this example I'll just do it if the mouse is down
    9.         isInPreviewMode = Input.GetMouseButton(0);
    10.  
    11.         //This might be new to you. It's just a nicer way of writing if statement,
    12.         renderer.material = isInPreviewMode ? previewMat : normalMat;
    13.     }
    and I guess maybe disable components like the box collider? etc