- Home /
Unity Prefab GameObject Instantly Dissapearing on Instantiation,Unity Prefab GameObject Dissapearing after Instantiation
Hey everyone, I am a beginner in Unity, but a fairly experienced coder. I'm having an issue with the engine.
I am working on a game where I would like the player to be able to slide across a screen and "shoot: a projectile. After a lot of research, I understand that I can do this using the "Instantiate" function. However, when I do call this method, objects appear on screen for half a second before dissapearing. As a test, I call this method in the Start() and another time when the player pressed a button that calls pressFire(). Both methods don't work, the prefab instantly dissapears.
There is a script attached to the prefab, but there's nothing other than "anchoredPosition += new Vecotr2(1,1) * Time.deltaTime;" in the Update() method.
I'm using a UI based system, where all my GameObjects are essentially Images. I also understand that using a RigidBody would make my life much easier, however I would like to see if I could finish the game by manipulating the RectTransform component.
public class TouchScript : MonoBehaviour
{
private bool firePressed = false;
public GameObject prefabProjectile;
public Transform panel;
private GameObject tempProj;
// Update is called once per frame
void Start()
{
tempProj = (GameObject) Instantiate(prefabProjectile, new Vector2(200f,200f), Quaternion.identity);
tempProj.transform.SetParent(panel.transform,false);
}
void Update()
{
if(Input.touchCount > 0)
{
if(firePressed)
{
fireRect.anchoredPosition = currentPos;
}
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
if(firePressed)
{
createProjectile(fireProjectile);
}
pressFire(false);
}
}
public void pressFire(bool b)
{
firePressed = b;
if(!b)
{
fireRect.anchoredPosition = fireOrignalPosition;
fireRect.GetComponent<TrailRenderer>().Clear();
}
}
public void createProjectile(Sprite s)
{
GameObject proj = (GameObject) Instantiate(prefabProjectile, new Vector2(0f,0f), Quaternion.identity);
proj.GetComponent<TouchPlayerProjectile>().setSprite(s);
proj.transform.SetParent(panel.transform,false);
}
}
Could anyone point out where I may have made a mistake or a better way of implementing my code?
Answer by logicandchaos · Jul 07, 2020 at 01:44 PM
When you say disappear what do you mean? Is it still in the scene hierarchy? Maybe the prefab is moving too fast and just jumps off the screen. Try it with the script disabled.
Answer by Swiindle · Jul 08, 2020 at 01:34 AM
@logicandchaos To clarify, the prefab appears in the scene hierachy and on the screen for ~0.5 seconds, before dissapearing. It's possible that the prefab is moving too fast, but I'm not sure what part of the code is making it do that.
I created a test:
I created a instance variable of an empty GameObject called TempProj
Within the update() method, I put a print statement that prints TempProj
I created a method that Instantiate() the prefab that I am using and stores the result into TempProj.
What happens is that after I press the button to instantiate, the GameObject appears for about 8 Update() cycles, before beco$$anonymous$$g null again. So: null > prefab Game Object (8 frames) > null