Search Unity

Prefab GameObject's can not be made active

Discussion in 'Scripting' started by Kitty, Apr 20, 2010.

  1. Kitty

    Kitty

    Joined:
    Apr 7, 2010
    Posts:
    11
    Things were going good till got the error above.

    I'm using the script below and set the element GameObject to a prefab. I thought doing this would mean I'd be finished with this part of my project and I could move to the next; however, running into this problem is leaving me a little stumped on what I could do to get the prefab to activate when the user clicks the cube and sets zoomedIn to true.

    I've been playing with Unity and scripting for a couple of months so any help would be greatly welcomed. :)

    Hierarchy:
    • 00-Script (Script below attached here)
      Table5
      >1
      >>Element (Prefab with children, needing to toggle active/disactive, tag "Element")


    Code (csharp):
    1. var zoomedIn : boolean;
    2. var defaultPosition : Vector3;
    3. var camOffset : Vector3;
    4. var mouseOver : String;
    5. var mouseStillOver : String;
    6. var element : GameObject;
    7.  
    8. function Start(){
    9.     targetPosition = myCamera.position;
    10. }
    11.  
    12. function Update () {
    13.     if(mouseStillOver==""){
    14.         if(Input.GetMouseButtonDown(0)){
    15.             if(zoomedIn){
    16.                 targetPosition = defaultPosition;
    17.                 zoomedIn = false;
    18.                 element.SetActiveRecursively(false);
    19.             }        
    20.             else{
    21.                 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    22.                 var hit : RaycastHit;
    23.                 if (Physics.Raycast (ray,hit, 50)) {
    24.                     //targetPosition = hit.point;
    25.                     var hitPosition = hit.transform.position;
    26.                     targetPosition = hitPosition+camOffset;
    27.                     zoomedIn =true;
    28.                     element.SetActiveRecursively(true);
    29.                     GameObject.FindGameObjectsWithTag("Element");
    30.                 }        
    31.             }    
    32.         }
    33.     }
    34.  
    35.    
    36.    
    37.     //Get GUI Tooltip and hold for "click-through" prevention
    38.     if(mouseOver==""){Invoke("ClearMouse",.15);}
    39.     else{mouseStillOver = mouseOver;}
    40.    
    41.  
    42.     myCamera.position += 0.25*(targetPosition-myCamera.position);
    43. }
    44.  
    45. //Get GUI Tooltip and hold for "click-through" prevention
    46. function ClearMouse(){
    47.     mouseStillOver = mouseOver;
    48. }
     
  2. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    I might be way off but I think what you want is
    Code (csharp):
    1.  
    2. var element : Transform;
    3.  
    Then in the inspector link your prefab to element.
    Hope that helps :)
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Afraid so...you can't set a Transform to active or inactive. Also, the actual problem is that you can't set a prefab to active or inactive either. You have to instantiate the prefab into your scene, then you can make the instance active/inactive. Or else link the "element" variable to an object that already exists in the scene.

    --Eric
     
  4. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    Lol Eric you the man!
    I actually was just searching for now what I realize the question was and your answer made it make sence!
    I really was way off lol :p
     
  5. Kitty

    Kitty

    Joined:
    Apr 7, 2010
    Posts:
    11
    Instantiate the prefab wouldn't work. I have it copied and attached to multiple children under the Table5 GO and have turned off certain mesh renderers to make each one different. So if I did go with that, I would have to figure out how set each one out and it could be a long process of for loops this noob isn't comfortable tackling.

    How would I go about doing this?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Drag the object from the scene onto the "element" slot.

    --Eric
     
  7. Kitty

    Kitty

    Joined:
    Apr 7, 2010
    Posts:
    11
    That's what I was thinking you'd say. I could do that; however, still only works for just that one object. Thinking I need to somehow get something that can tell which element is inside the cube clicked on in order to get the object active. Something like using the camera position to apply the hide/show function with that perhaps??
     
  8. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
    To activate all children of a gameObject, use:
    Code (csharp):
    1.  
    2.  
    3.     YourGameObject.SetActiveRecursively(true);
    4.  
    5.  
    I actually have a related problem. I am calling functions in a script which turn on and off a game object using:

    Code (csharp):
    1.  
    2. function1 () {
    3. MyGameObject.active = false;
    4. }
    5.  
    6. function2 () {
    7. MyGameObject.active = true;
    8. }
    9.  
    The object is deactivated in Start function, activated with function2, and that works fine. BUT, after I call function 1 and deactivate the object again, **it can never be reactivated.**. In the Hierarchy view it looks like it has become active again, but the Update function in the script attached to the object doesn't work!!

    Can anyone help?
     
  9. vished

    vished

    Joined:
    Oct 24, 2010
    Posts:
    26