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

Replace gameobject with another

Discussion in 'Scripting' started by psyc0rt, Jun 22, 2015.

  1. psyc0rt

    psyc0rt

    Joined:
    Aug 9, 2013
    Posts:
    5
    Hi,
    I've a javascript on a button. This button should be able to replace a gameObject:
    1. how can I hide a gameObject (3D Model) from the start, and make it later visible by a buttonclick ?
    2. how can I replace this model again by a further buttonclick to another gameObject ?

    I would be really grateful if you can help me.
    My version is Unity3D 5.0

    Best Regards,
    Dave
     
    Last edited: Jun 22, 2015
  2. Junglebiscuit

    Junglebiscuit

    Joined:
    Mar 21, 2015
    Posts:
    6
  3. psyc0rt

    psyc0rt

    Joined:
    Aug 9, 2013
    Posts:
    5
    Yes I used google but i guess my programming skills are to bad and I hope that u can give my some help. I tried your link but unfortunately it doesnt work hope somebody could help me.
    This java script runs on a button, but how do the function know which gameObject
    is meant I tried for example:

    function Change () {

    gameObject.GetComponent("tankTwo");
    public var rend: gameObject.GetComponent("tankTwo");
    function Start() {
    rend = GetComponent.<"tankTwo">();
    rend.enabled = false;
    }

    }__________________

    #pragma strict
    function Update() {

    if (Input.GetKeyDown(KeyCode.Z)) {
    // show
    renderer.enabled = true;
    }

    if (Input.GetKeyDown(KeyCode.X)) {
    // hide
    renderer.enabled = false;
    }

    }
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I really do recommend looking at the basic scripting tutorials in the learn section...

    http://unity3d.com/learn/tutorials/modules/beginner/scripting

    you're making some very simple mistakes which you should be able to find and sort out once you have gone through the basics.


    also, when you paste code into the forums please use [ code] [/ code] tags, it helps with the readability, there is a sticky on them at the top of the scripting forum.