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

Instantiate not working javascript

Discussion in 'Scripting' started by TheFrozenFires, Jul 28, 2013.

  1. TheFrozenFires

    TheFrozenFires

    Joined:
    Jul 5, 2013
    Posts:
    8
    Hello

    I've been following the Walker boys tutorials and having a great time learning unity! However in the API and first game videos, when I go to make an instance of an object with Instantiate it doesn't work.

    I'm using MonoDevelop and Unity 4.2.0 on OSX 10.7.5

    Right now I'm trying to make an explosion instatiate and it doesn't come up. I've followed this tutorial exactly

    http://vimeo.com/18907747

    Here is my code

    Code (csharp):
    1. #pragma strict
    2.  
    3. //Enemy Script
    4. //July 28 2013
    5.  
    6. //Inspector Variables
    7. var numberOfClicks  : int = 1;
    8. var respawnWaitTime : float = 3.0;
    9. var shapeColor      : Color[];
    10. var explosion       : Transform;
    11.  
    12. //Private Variables
    13. private var storeClicks : int = 0;
    14.  
    15. //Start is only called once in lifetime of behavior
    16. function Start ()
    17. {
    18.     storeClicks = numberOfClicks;
    19. }
    20.  
    21. function Update ()
    22. {
    23.     if(numberOfClicks <= 0)
    24.     {
    25.         if (explosion == true)
    26.         {
    27.             Instantiate (explosion, transform.position, transform.rotation);    //create an explosion
    28.         }
    29.        
    30.         var position = Vector3 (Random.Range(-6,6), Random.Range(-4,4), 0); //new random position for game object
    31.         RespawnWaitTime();
    32.         transform.position = position;  //move the game object to the new position
    33.         numberOfClicks = storeClicks;
    34.     }
    35.    
    36. }
    37.  
    38. // RespawnWaitTime is used to hide the game object for a set amount of time, then reaapear
    39. function RespawnWaitTime ()
    40. {
    41.     renderer.enabled = false;
    42.     RandomColor ();
    43.     yield WaitForSeconds (respawnWaitTime);
    44.     renderer.enabled = true;
    45. }
    46.  
    47. // RandomColor is used to change the material of game object
    48. function RandomColor ()
    49. {
    50.     if (shapeColor.Length > 0)
    51.     {
    52.         var newColor = Random.Range(0, shapeColor.length);
    53.         renderer.material.color = shapeColor[newColor];
    54.     }
    55. }
    Any and all help appreciated! :)
     
  2. meta87

    meta87

    Joined:
    Dec 31, 2012
    Posts:
    254
    I'm not familiar with the tutorial, but what are you trying to do with this line?

    if (explosion == true)

    Explosion is a Transform, not a boolean so I don't think that will work. Also, I don't see where you are increasing the storeClicks variable if you actually click with your mouse.

    Is that the full script?
     
  3. TheFrozenFires

    TheFrozenFires

    Joined:
    Jul 5, 2013
    Posts:
    8
    This is the full script controlling the 'enemies'

    I actually solved this by making it the following:

    if (explosion)

    so that was definitely the problem :)

    storeClicks is just a private variable to hold the starting amount of clicks to kill the enemy for when a new enemy is brought in.

    Thanks for the help!
     
  4. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250

    if(transform) and if(!transform) do work.