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

Instantiated child prefab invisible

Discussion in 'Scripting' started by blober81, Aug 14, 2016.

  1. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Hi everyone,

    When I load a prefab sprite using...


    Code (csharp):
    1.  
    2. GameObject g =Instantiate(Resources.Load("Prefabs/"+ unitName [0])) as GameObject;
    3.  
    ...the child object is rendered in wireframe. When searching for answers there were many posts on this subject. However none of the solutions fixed my problem.

    • the instantiation is not done in Awake() so that can't be the problem.

    • The child prefab is not rotated during the instantiation.

    • I changed the sorting layer of both the parent and child prefab but the child remained invisble.

    • The order in layer of the parent prefab is 2 and the child is 3 so that should be OK as well.

    • In scene view I am able to click on the child prefab. So during instantiation it's placed in the right position and rotation.

    • When I manually drag the prefab into the scene it's displayed correctly.
    Does anyone have a clue what could be the problem here ? Thanks so much for any help.
     
    Last edited: Aug 14, 2016
  2. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Offcourse I could just make a public gameobject and attach my prefab in the inspector but when you have alot of prefabs and you only need a few it becomes inconvenient. Thats why I'd rather load the prefabs I need. For some reason however the child object on my prefab is rendered in wireframe.

    My wild guess is that it's something in the code because my settings in the inspector are the same as for when I drag the prefab in the screen. Because when I drag the prefab into the scene view the prefab child is visible.
     
    Last edited: Aug 14, 2016
  3. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Ok I found out that when I delete the only script on my child object the child object is visible when I import it using resources.load but I have no idea how my script could effect the visibility of my child object.
     
  4. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Does anyone have any clue what could be the problem here ? It's been a few days already I'm struggling with this. Everything in the instantiated prefab works except for the fact that it doesn't render the child object.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    going to be a complete guess; but something in that script?

    you've not really provided anything for us to work with so its hard to help.
     
  6. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    using ; Resources.Load("Prefabs/Tank"

    When I run the script on my child object the child is rendered in wireframe.
    When I disable the child script in inspector I have the same result.
    When I remove the child script the child object is being displayed like it should.

    here is the turret script which is attached to the child object; For some reason the spaces are left out...

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. publicclassTurret:MonoBehaviour{
    7.  
    8.  
    9. Collider2D[]colliders=newCollider2D[5];
    10.  
    11. publicLayerMaskP1mask;
    12. publicLayerMaskP2mask;
    13. publicLayerMaskUseMask;
    14.  
    15. publicLayerMaskP1Obstacle;
    16.  
    17.  
    18.  
    19. publicfloatradiusC=1f;
    20.  
    21. GameObjectTargetEnemy=null;
    22. GameObjectTempTargetEnemy=null;
    23.  
    24. //coroutinesomframeratedropste berperken
    25. privateboolBDetectEnemies=false;
    26. privateboolBSeekAndDestroy=false;
    27.  
    28.  
    29. //checkvoordichtsbijzijnde vijand
    30. //floatdistance=Mathf.Infinity;
    31.  
    32.  
    33. //---------rotation parameters
    34. //publicTransformTarget;
    35. publicfloatRspeed;
    36.  
    37. //---------raycasttocheckiftargetislocked on
    38. privatefloatmaxRayDistance=5.1f;
    39.  
    40. //collider raycast
    41. GameObjectTempCol=null;
    42.  
    43.  
    44. ////--------shootingshells/ parameters
    45. publicGameObjectBullet_Emitter;
    46.  
    47. stringtankName;
    48. stringshellname;
    49.  
    50.  
    51.  
    52.  
    53. //delayshooting parameters
    54. publicfloatfireRate=1.5f;
    55. privatefloatnextFire=0.0f;
    56.  
    57.  
    58.  
    59. public float Movespeed=1;
    60.  
    61.  
    62.  
    63. void Start()
    64. {
    65. tankName=this.transform.parent.name;
    66.  
    67.  
    68. }
    69.  
    70.  
    71.  
    72. publicvoidSetMask(intmasknr)
    73. {
    74.  
    75. if(masknr==1){
    76. UseMask=P1mask;
    77. //UseMask=1;
    78. }
    79. else
    80. {
    81. UseMask=P2mask;
    82. //UseMask=2;
    83. }
    84. }
    85.  
    86.  
    87.  
    88. public void SetLayer(int layernr)
    89. {
    90. //Debug.Log("layernR"+layernr);
    91. if(layernr==1){
    92. this.gameObject.layer=8;
    93. }
    94. else
    95. {
    96. this.gameObject.layer=9;
    97. }
    98.  
    99. }
    100.  
    101.  
    102.  
    103.  
    104. voidUpdate(){
    105.  
    106.  
    107. Debug.DrawLine(transform.position,transform.position+transform.up*maxRayDistance);
    108. TargetEnemy=TempTargetEnemy;
    109.  
    110. if(BDetectEnemies==false)
    111. {
    112. StartCoroutine(CRDetectEnemies(0.33f));
    113. BDetectEnemies=true;
    114. }
    115.  
    116.  
    117. if(TargetEnemy !=null){
    118. Vector3targetDir=TargetEnemy.transform.position-transform.position;
    119. transform.up=Vector3.RotateTowards(transform.up,targetDir,(Rspeed*Mathf.Deg2Rad*Time.deltaTime),0f);
    120.  
    121. if(BSeekAndDestroy==false)
    122. {
    123. StartCoroutine(CRSeekandDestroy(0.33f));
    124. BSeekAndDestroy=true;
    125. }
    126. }
    127.  
    128. }
    129.  
    130.  
    131. voidDetectEnemies()
    132. {
    133.  
    134. TempTargetEnemy=null;
    135.  
    136. Collider2D[]colliders=(Physics2D.OverlapCircleAll(transform.position,radiusC,UseMask));
    137.  
    138. floatdistance=Mathf.Infinity;
    139. foreach(Collider2D col incolliders){
    140.  
    141. if(Physics2D.Linecast(transform.position,col.gameObject.transform.position,P1Obstacle)){
    142.  
    143. }
    144. else
    145. {
    146. Vector3position=transform.position;
    147. Vector3diff=col.transform.position-position;
    148. floatcurDistance=diff.sqrMagnitude;
    149. Debug.DrawLine(transform.position,col.gameObject.transform.position,Color.red);
    150.  
    151.  
    152. if(curDistance<distance)
    153. {
    154. TempTargetEnemy=col.gameObject;
    155. distance=curDistance;
    156. }
    157.  
    158. }
    159. }
    160. MoveSpeed();
    161. }
    162.  
    163.  
    164. voidSeekAndDestroy()
    165. {
    166. RaycastHit2Dhit=Physics2D.Raycast(transform.position,transform.up,maxRayDistance,UseMask);
    167. Debug.DrawLine(transform.position,transform.position+transform.up*maxRayDistance);
    168. if(hit.collider !=null)
    169. {
    170.  
    171. if(hit.collider.gameObject.name==TargetEnemy.gameObject.name){
    172. if(Time.time>nextFire){
    173. nextFire=Time.time+fireRate;
    174.  
    175. GameObject.Find(shellname).GetComponent<shellDamage>().FireBullet();
    176.  
    177.  
    178. }
    179. }
    180. }
    181.  
    182. }
    183.  
    184.  
    185.  
    186.  
    187. publicvoidGetBulletName(stringshelll)
    188. {
    189. //Debug.Log("shellname?:"+shelll);
    190. shellname=shelll;
    191. }
    192.  
    193.  
    194.  
    195. publicvoidSetcolor(ColorDocolor)
    196. {
    197. this.GetComponent<SpriteRenderer>().color=Docolor;
    198. }
    199.  
    200.  
    201.  
    202.  
    203.  
    204.  
    205. //
    206. voidMoveSpeed()
    207. {
    208.  
    209. //Debug.Log("MoveCheck");
    210. if(TargetEnemy==null){
    211. Movespeed=1;
    212. GameObject.Find(tankName).GetComponent<Tank>().stopForTarget(Movespeed);
    213. }
    214. else
    215. {
    216. Movespeed=0;
    217. GameObject.Find(tankName).GetComponent<Tank>().stopForTarget(Movespeed);
    218. }
    219.  
    220.  
    221. }
    222. //
    223.  
    224.  
    225.  
    226. //coroutines
    227.  
    228. IEnumeratorCRDetectEnemies(floatWaitTime)
    229. {
    230. yieldreturnnewWaitForSeconds(WaitTime);
    231. BDetectEnemies=false;
    232. DetectEnemies();
    233. }
    234.  
    235.  
    236.  
    237. IEnumeratorCRSeekandDestroy(floatWaitTime2)
    238. {
    239. yieldreturnnewWaitForSeconds(WaitTime2);
    240. SeekAndDestroy();
    241. //Debug.Log("CRSEEKANDDESTROY!!");
    242. BSeekAndDestroy=false;
    243. }
    244.  
    245.  
    246. }
    247.  
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how is "SetColor" being called, and does the color being passed have 0 alpha?
     
    blober81 likes this.
  8. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    SetColor is called from UnitBuild() in the same script. How can I see if the color passed has 0 alpha ?
     
  9. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Hey I removed the color and now the child object is visible. I'll probably can figure it out now. Thanks so much for your help !

    So now I call SpawnUnit() and not SpawnUnit(P1Color)

    Probably I messed a few things up since I'm not calling the SpanUnit method from a fdifferent script and I forgot to add the color values in the script in which the SpawnUnit method is called.

    Cheers !
     
    LeftyRighty likes this.