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. Dismiss Notice

Prefab references always point to first instantiated prefab-c#

Discussion in 'Scripting' started by 1337GameDev, Apr 24, 2012.

  1. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I have a loop that instantiates ship objects for a rts game im making. I have a selector script that selects references of objects based upon a raycast hit operation. When i have one ship (without instantiating others) my code for selecting works fine, and so does deselecting code(i loop through all ship tagged objects and see if they have the selection obj attached to them) but when i just instantiate a few more ship prefabs, my code breaks.

    I touch any ship and it selects the first ship instantiated, not the one i touch or my original one in the scene. I use a debug string to output the obj name selected, and if i tap the original (which is named MainShip1) it shows the correct name of the object but selects a prefab instantiation rather than the ship hit by the raycast. If i tap any prefab instantiation it selects the first prefab aswell that is instantiated but shows the name "MainShip1(Clone)" which is what it would under instantiation of a prefab.

    Here are images of my scene and problem (notice the debug output too in the upper left):

    This is when i select my ship without other prefabs instantiating:

    Here is deselecting:


    Here is before i select a ship when i have other prefabs instantiated:

    Here is after i select a ship (The one to the left of the white cube which is my original and the selected ship is the first prefab instantiated)


    Here is my instantiation code:

    Code (csharp):
    1.  
    2.         Ship1 = (GameObject)Resources.Load("MainShip1");
    3.        
    4.         for(int i = 0;i<8;i++)
    5.         {
    6.             GameObject ship = (GameObject)Instantiate(Ship1, new Vector3(-1435.566F + (i*7F),0,-1427.19F), new Quaternion(0,0,0,0) );
    7.             ship.tag = "Ships";
    8.             GameObject hull = ship.transform.Find("ShipHull").gameObject;
    9.             ShipVars shipVars = hull.GetComponent("ShipVars") as ShipVars;
    10.             shipVars.currentCommand = 0;
    11.             debugGText.permDebug("ship number " + i + " is named " + ship.gameObject.name + "and hullObj is named: " + hull.name + " and is on layer " + ship.layer + "and has tag " + ship.tag );
    12.         }

    Here is my code for my raycast.hit code:

    Code (csharp):
    1.                 if ((hit.collider)  hit.collider.gameObject.transform.parent.gameObject.CompareTag("Ships")) //if my ray hit a "Ship" tagged object (collider is component and this references the object it is attached to?
    2.                 {
    3. //change to raycast all, as it stores an array of all objects hit and ray doesnt stop at first one 
    4. //or disable invisPlane collider until raycast finishes, then if no other colliders are hit reenable invisPlane collider and reraycast to determine move point.            
    5.                     //debugGText.setDebug("callingSelObjMethod");
    6.                     //debugGText.permDebug("selecting ship from touch");
    7.                     debugGText.permDebug("hitObj name: " + hit.transform.name);
    8.                     selectThisObj(hit.transform.gameObject); //references hull
    9.                    
    10.                 }
    And here is my selectThisObj method:

    Code (csharp):
    1.  
    2.     public void selectThisObj(GameObject g)
    3.     {
    4.         g = GameObject.Find("ShipHull");
    5.         //debugGText.permDebug("insideSelObjMethod, gameObj name is: " + g.name);
    6.         ShipVars script = g.GetComponent("ShipVars") as ShipVars;
    7.         //debugGText.setDebug("after ShipVars referencing....");
    8.         if(script != null)
    9.         {
    10.             //debugGText.setDebug("script isnt null");
    11.             if(script.isSelected == false)
    12.             {
    13.                 //debugGText.permDebug("SelectThis obj: script != null, isSelected var = f, selecting....");
    14.                 script.isSelected = true;
    15.                 selObjList.Add(g);
    16.                
    17.                 //create selected obj here and parent to selected obj ship
    18.                 GameObject selectedObjGui = (GameObject)Instantiate(guiSel, g.transform.position, g.transform.rotation); //instantiate a prefab of my selection object at the colliders current coordinates                            
    19.                 script.selGuiObj = selectedObjGui;
    20.                 debugGText.permDebug("inSelectObj method and attaching selObj to: " + g.collider.gameObject.name + " and shipObj is " + g.collider.transform.parent.gameObject.name);
    21.                 selectedObjGui.transform.parent = g.collider.transform;
    22.                 selectedObjGui.transform.localPosition = Vector3.zero;
    23.                 selectedObjGui.transform.localRotation = Quaternion.identity;
    24.                 biggestDimension = script.biggestDimension;
    25.                 selectedObjGui.transform.localScale = new Vector3((float)(biggestDimension/3.0F), (float)(biggestDimension/3.0F), 1.0F);
    26.             }
    27.         }
    28.         else
    29.         {
    30.             //debugGText.setDebug("script is null");
    31.         }
    32.         somethingSelected = true;
    33.         //debugGText.permDebug("calling populate list");
    34.         scrollListVert.populateList(2);
    35.     }
    36.  
    Here is my ship prefab hierarchy:



    Well i hope i didn't scare anybody away with providing code and images. I just am super confuse don how to debug this and why this would happen.... :confused:
     
  2. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    Could it be the isSelected variable messing with you?

    I mean, it doesn't look like you ever set it to false, so when selecting something else it remains true.

    Good luck!
     
  3. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I fixed it, it was my code that was fetch the ShipHull child object, it was fetching the first ShipHull form ALL the objects in scene, not just game object i was looking at. I changed it to g.transform.Find("ShipHull").gameObject to get the child.

    But now my code only works for my main ship object and skips code that would execute for deselecting objects when i have a prefab selected. Why would this be, if it works for the First object, but not prefabs? I made sure i wasnt using a global find operation ....
     
    Last edited: Apr 25, 2012
  4. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54