2

I have a problem with instantiating game object in Unity. So the game is 2D, and this piece of code is for my collider hit. Part of it is in charge of instantiating an object at position where the ray has hit the collider. And it does, when i run the game and ray hits the collider, instatiated object appears in hierarchy but not visible on scene and in debug, value of tmpCircle is null, here is the code:

if (result.collider.CompareTag("Images"))
                {                    
                    Debug.Log("Hit");
                    result.collider.enabled = false;                    
                    GameObject tmpCircle = GameObject.Instantiate(OrgCircle, result.point, Quaternion.identity);
                    tmpCircle.SetActive(true);   
                    tmpCircle.transform.position = new Vector3(result.point.x, result.point.y, result.point.z);
                    CircleAnimation.Play("CircleAnim");
                    score += 1;
                    differences -= 1;
                    coins += 1;
                    score1.text = " " + score;
                    object.Play();
                }

  • 1
    Do you have smg in OrgCircle and what the type for it? The line for position is redundant since it's same as Instantiate.. – Everts Mar 12 '21 at 21:34
  • I'm sorry, i'm quite new to Unity, what is smg? And for position, yes, i know, i've realized that after posting this. – Juan Don Guterez Mar 12 '21 at 21:44
  • My bad, smg = something. Is there a prefab for OrgCircle? I guess you dragged a prefab in the inspector. – Everts Mar 13 '21 at 08:18
  • I have tried both options, dragging in the inspector and using GameObject.Find, i just can't figure out what the value is null – Juan Don Guterez Mar 13 '21 at 12:31
  • No answers in the question, please. I have rolled back/edited your question and removed the answer. Add the answer in the answer section only. – Sabito stands with Ukraine Mar 13 '21 at 13:35
  • Is your prefab a UI element (e.g. `UnityEngine.UI.Image`)? In this case it will not be visible if is not placed in the Hierarchy nested under a `UI.Canvas` ... – derHugo Mar 13 '21 at 17:10
  • Yes, i've solved the problem, the instantiated object wasn't child of canvas – Juan Don Guterez Mar 13 '21 at 20:23

1 Answers1

0

Perhaps since its 2D, the sprite component is drawing behind other sprites. Try increasing the z index. Or the sprite renderer component is not enabled in your prefab. Check the scale of the sprite.

  • Well it's not a sprite, it's an image with animation and yes i have tried increasing and decreasing the z index in range -100 to 100, not working. I can't figure out why the value of clone object is null in debug – Juan Don Guterez Mar 13 '21 at 12:33