Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Swiindle · Jul 07, 2020 at 09:42 AM · gameobjectinstantiateprefabdisappearingdissapear

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Swiindle · Jul 08, 2020 at 06:45 AM 0
Share

I created a test:

  1. I created a instance variable of an empty GameObject called TempProj

  2. Within the update() method, I put a print statement that prints TempProj

  3. 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference Player When Instantiating A Prefab 0 Answers

Issues with destroying a game object 2 Answers

Add 0.5f to Vector.Right on next instantiated object ? 1 Answer

Instantiated Object being auto-deleted 0 Answers

How to instantiate within GameObject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges