Search Unity

Instancing/Referencing help

Discussion in 'Scripting' started by terminal205, Mar 1, 2015.

  1. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    I am attempting to reference/instance a game object when OnMouseDown conditions are met. The following code is the general idea of what I'm trying to do:

    Right now I get an error about the object not being referenced

    Code (javascript):
    1.  
    2.  
    3. var onHoverImage : Texture;
    4. var onClickImage : Texture;
    5. var iconQuad : GameObject;
    6. var hoverColor : Color;
    7. var clickColor : Color;
    8. var linkObject : GameObject;
    9.  
    10.  
    11. private var parentColor;
    12. private var pTransform : Transform;
    13. private var childCount: int;
    14. private var offColor : Color;
    15. private var offImage : Texture;
    16. private var selected : boolean;
    17. private var objCollider : Collider;
    18.  
    19. private var numChild : int;
    20. private var segLength : float = 12.00;
    21. private var nodeDistance : float;
    22.  
    23. function OnMouseDown()
    24. {  
    25.    var tempArray : GameObject[];
    26.    var tempArrayIndex : int = 0;
    27.    var tObject : Transform;
    28.    var child : int ;
    29.    var parentPos : Vector3 = gameObject.transform.position;
    30.    
    31.    if(selected == false)
    32.      selected = true;
    33.    else if(selected == true)
    34.    {
    35.      selected = false;
    36.      iconQuad.renderer.material.mainTexture = offImage;
    37.      renderer.material.color = offColor;
    38.    }
    39.  
    40.    renderer.material.color = clickColor;
    41.    iconQuad.renderer.material.mainTexture = onClickImage;
    42.  
    43.    for each(Transform in transform)
    44.    {
    45.      
    46.      tObject = Transform;
    47.          
    48.      if(Transform != iconQuad.transform)
    49.      {
    50.        
    51.        if(selected == true)
    52.        {
    53.          tObject.gameObject.SetActive(true);
    54.          // Get Distance to child object
    55.          nodeDistance = Vector3.Distance(tObject.position, transform.position);
    56.          print("Node Distance : " + nodeDistance);
    57.          // Instantiate linkObject; object length (z) is 12
    58. //         tempArray[tempArrayIndex] = Instantiate(linkObject, transform.position,transform.rotation);
    59.          tempArray[tempArrayIndex] = PrefabUtility.InstantiatePrefab(linkObject as instance);
    60.          //Debug.Log(Path.GetFileNameWithoutExtension(linkObject));
    61.          // Rotate to face child
    62.          tempArray[tempArrayIndex].transform.LookAt(tObject.position);
    63.        
    64.        
    65.          tempArrayIndex++;
    66.        }
    67.        else if(selected == false)
    68.        {
    69.          tObject.gameObject.SetActive(false);
    70.        
    71.        }
    72.  
    73.      }
    74.      
    75.    }
    76.    
    77. }
    78.  
    All help would be very much appreciated
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Transform is a class name, similar to Vector3.

    On line 46 you are saing tObject = Transform (making it equal to a class).
    On line 53 you are then saying tObject.gameObject, trying to access the gameObject from the Transform class, which will give you problems because Transform is just a class, and not a transform object that will have a gameObject. (similar to saying Vector3.x)

    I don't use foreach often, but you should double check the correct way to use it.

    Goodluck!
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Error line number?
     
  4. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Line 59 of this code.