Search Unity

Spawning Objects

Discussion in 'Scripting' started by terminal205, Apr 5, 2015.

  1. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    I thought I had was making progress in my understanding of Unity Script, but apparently I was wrong in that assumption.

    I am attempting to "spawn"/instance game objects, orient, and scale game objects after clicking on another object in the scene. At the same point, clicking on the same object again will de-instance/remove the object from the scene.

    How would I go about doing this? What scripting references should I look at so I can get a pointed in the right direction ?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    I get this error after Clicking on an object in the game. I have no idea what this error means. Any help?
    Code (javascript):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. (wrapper stelemref) object:stelemref (object,intptr,object)
    4. menuMouseActions.OnMouseDown () (at Assets/scripts/menuMouseActions.js:106)
    5. UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
    6.  
    The error appears on line 106:
    Code (javascript):
    1.  
    2. instanceArray[arrayIndex] = Instantiate(linkObject, transform.position,transform.rotation);
    3.  
    At this point in the script, arrayIndex is 0, and I have just created my first object via the Instantiate command.
     
  4. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Here is the OnMouseDown() function in its entirety:
    Code (javascript):
    1.  
    2. function OnMouseDown()
    3. {  
    4.    var instanceArray : GameObject[];
    5.    var arrayIndex : int = 0;
    6.    var tObject : Transform;
    7.    var child : int ;
    8.    var parentPos : Vector3 = gameObject.transform.position;
    9.  
    10.    if(selected == false)
    11.      selected = true;
    12.    else if(selected == true)
    13.    {
    14.      selected = false;
    15.      iconQuad.GetComponent.<Renderer>().material.mainTexture = offImage;
    16.      GetComponent.<Renderer>().material.color = offColor;
    17.    }
    18.  
    19.    GetComponent.<Renderer>().material.color = clickColor;
    20.    iconQuad.GetComponent.<Renderer>().material.mainTexture = onClickImage;
    21.  
    22.    for each(Transform in transform)
    23.    {
    24.    
    25.      tObject = Transform;
    26.        
    27.      if(Transform != iconQuad.transform)
    28.      {
    29.           if(selected == true)
    30.        {
    31.          tObject.gameObject.SetActive(true);
    32.        }
    33.        else if(selected == false)
    34.        {
    35.          tObject.gameObject.SetActive(false);
    36.        }
    37.      
    38.          // Get Distance to child object
    39.          nodeDistance = Vector3.Distance(tObject.position, transform.position);
    40.          print(tObject + " distance : " + nodeDistance);
    41.          // Instantiate linkObject; object length (z) is 12
    42.          instanceArray[arrayIndex] = Instantiate(linkObject, transform.position,transform.rotation);
    43.  
    44.          // Rotate to face child
    45.          print(tObject + ".position is : " + tObject.position);
    46.          instanceArray[arrayIndex].transform.LookAt(tObject.position);
    47.  
    48.      
    49.          arrayIndex++;
    50.  
    51.      }
    52.    }
    53. }
    54.  
    I added a bit of error check-point code to see where the error as being thrown. I added the code on line 45, but the script errors out before it hits that. Guess this means I'm screwing something up with the line above it.

    Is there a better/smarter way to store a list of instantiated objects so I can modify and later destroy them?
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    instanceArray is null, you can't access an index of a null array! I'm unsure how to actually declare an array in javascript but I'm guessing that's your problem. In C# you would say,

    instanceArray = new GameObject[10];...which initializes with a size of 10 that array.

    For you, if you don't know how many you will need, I would use the Array class.

    http://docs.unity3d.com/ScriptReference/Array.html
     
  6. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Updated code:
    Code (javascript):
    1.  
    2. #pragma strict
    3. import System.IO;
    4.  
    5. var onHoverImage : Texture;
    6. var onClickImage : Texture;
    7. var iconQuad : GameObject;
    8. var hoverColor : Color;
    9. var clickColor : Color;
    10. var linkObject : GameObject;
    11.  
    12.  
    13. private var parentColor;
    14. private var pTransform : Transform;
    15. private var childCount: int;
    16. private var offColor : Color;
    17. private var offImage : Texture;
    18. private var selected : boolean;
    19. private var objCollider : Collider;
    20.  
    21. private var numChild : int;
    22. private var segLength : float = 12.00;
    23. private var nodeDistance : float;
    24.  
    25.  
    26.  
    27.  
    28. function Start ()
    29. {
    30.    offImage = iconQuad.GetComponent.<Renderer>().material.mainTexture;
    31.    selected = false;
    32.    offColor = GetComponent.<Renderer>().material.color;
    33. }
    34.  
    35.  
    36. function OnMouseEnter()
    37. {
    38.    if(selected == false)
    39.    {
    40.      GetComponent.<Renderer>().material.color = hoverColor;
    41.      iconQuad.GetComponent.<Renderer>().material.mainTexture = onHoverImage;
    42.    }
    43. }
    44.  
    45. function OnMouseExit()
    46. {
    47.    if(selected == false)
    48.    {
    49.      GetComponent.<Renderer>().material.color = offColor;
    50.      iconQuad.GetComponent.<Renderer>().material.mainTexture = offImage;
    51.      
    52.    }
    53. }
    54.  
    55.  
    56. function OnMouseDown()
    57. {  
    58.    var instanceArray = new Array ();
    59.    var arrayIndex : int = 0;
    60.    var tObject : Transform;
    61.    var child : int ;
    62.    var parentPos : Vector3 = gameObject.transform.position;
    63.    
    64.    if(selected == false)
    65.      selected = true;
    66.    else if(selected == true)
    67.    {
    68.      selected = false;
    69.  
    70.      iconQuad.GetComponent.<Renderer>().material.mainTexture = offImage;
    71.      GetComponent.<Renderer>().material.color = offColor;
    72.    }
    73.  
    74.    GetComponent.<Renderer>().material.color = clickColor;
    75.    iconQuad.GetComponent.<Renderer>().material.mainTexture = onClickImage;
    76.  
    77.    for each(Transform in transform)
    78.    {
    79.      tObject = Transform;
    80.          
    81.      if(Transform != iconQuad.transform)
    82.      {
    83.        if(selected == true)
    84.        {
    85.          tObject.gameObject.SetActive(true);
    86.          // Get Distance to child object
    87.          nodeDistance = Vector3.Distance(tObject.position, transform.position);
    88.          instanceArray[arrayIndex] = Instantiate(linkObject, transform.position,transform.rotation);
    89.          // Rotate to face child
    90.          var tempObj = new GameObject();
    91.          tempObj =  instanceArray[arrayIndex];
    92.          //tempObj.transform.localScale.z = -1;
    93.          //tempObj.transform.localScale.x = 0.1;
    94.          //tempObj.transform.localScale.y = 0.1;
    95.          //tempObj.transform.localScale.z = 0.1;
    96.          tempObj.transform.LookAt(tObject.transform.position, Vector3(0,1,0));
    97.  
    98.          arrayIndex++;
    99.        }
    100.        else if(selected == false)
    101.        {
    102.          for(var obj : GameObject in instanceArray)
    103.          {
    104.            Destroy(obj);
    105.            print(obj + " destroyed!");
    106.          }
    107.          tObject.gameObject.SetActive(false);
    108.        }
    109.        
    110.          
    111.  
    112.      }
    113.    }
    114. }
    115.  
    116.  
    117.  
    So all the objects are instanced, and now I can manipulate them. The issue I'm running into now is destroying them when I click on the parent object again. For some reason it fails to destroy the instances when "deselecting" the object.