Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Sek19 · Nov 21, 2016 at 09:58 PM · guiinstantiateprefab

problem with instantiated clones not running OnEnterTrigger / OnexitTrigger script?

Hello, I have been looking around the forums about a problem I'm having with instantiating a prefab with a script attached. I'm pretty sure it has to do with the way I'm structuring my code either in the class or having my class try to handle too much script into one class type.

I have two objects, one that can be captured and the other that is instantiated. When capturing starts (capture loading bar) the player has entered the trigger and stays within this trigger. The object counts down 5f and instantiates the GUI prefab that displays this to the user. The issue is that the cloned loading bar never fills the bar unless all the loading bar code is inside the Update function. I have the same exact prefab within my scene sitting to the side and it will fill the loading bar with all loading bar code sitting in my OnTriggerStay function but when it is cloned / instantiated I have no action taking place.

When you plan to make a GUI element or any object instantiate itself, what is the best practice for making sure the script attached is running when instantiated? Especially when you have code outside of the Update or FixedUpdate function?

I found that if I place the code I have for the loading bar in the Update function it runs, is this where my code should be and I just create a if (trigger = true) fill bar / else detroy object to check to see if the loading bar should be active? I would like to keep the code within my OnTriggerEnter Function but am I doing something wrong in the code?

this is the code that works only for the prefab that is currently residing in the scene

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Capture : MonoBehaviour {
 
     float countdown = 5f;
     public Transform LoadingBar;
     //public Transform prefab;
     public Transform prefab2;
     public GUI guiprefab;
     public Transform position;
     [SerializeField] private float currentAmmount;
 
     // Update is called once per frame
     void Update()
     {
     }
 
     void OnTriggerEnter (Collider other)
     {
         Transform prefab;
         prefab = Instantiate(prefab2, position.position, position.rotation) as Transform;
     }
     void OnTriggerStay (Collider other)
     {
         if (other.attachedRigidbody)
         {
 
             
             //Instantiate (prefab, position.position, position.rotation);
             currentAmmount += countdown * Time.deltaTime * 8;
             countdown -= Time.deltaTime;
             Debug.Log(currentAmmount);
             LoadingBar.GetComponent<Image>().fillAmount = currentAmmount / 100;
             if (countdown <= 0.0f)
             {
                 GetComponent<Renderer>().material.color = Color.red;
             }
            
         }
     }
 
     void OnTriggerExit (Collider other)
     {
         Color team = GetComponent<Renderer>().material.color;
         if (team == Color.red)
         {
             countdown = 5f;
         }
         else
         {
             countdown = 5f;
             GetComponent<Renderer>().material.color = Color.blue;
         }
     }
 }

 

if I move the code for the loading bar within the update function it works for the instantiated object, but like I mentioned I would like to find a way to keep it within the OnTriggerStay.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Capture : MonoBehaviour {
 
     float countdown = 5f;
     public Transform LoadingBar;
     //public Transform prefab;
     public Transform prefab2;
     public GUI guiprefab;
     public Transform position;
     [SerializeField] private float currentAmmount;
 
     // Update is called once per frame
     void Update()
     {
         currentAmmount += countdown * Time.deltaTime * 8;
         countdown -= Time.deltaTime;
         Debug.Log(currentAmmount);
         LoadingBar.GetComponent<Image>().fillAmount = currentAmmount / 100;
     }
 
     void OnTriggerEnter (Collider other)
     {
         Transform prefab;
         prefab = Instantiate(prefab2, position.position, position.rotation) as Transform;
     }
     void OnTriggerStay (Collider other)
     {
         if (other.attachedRigidbody)
         {
             countdown -= Time.deltaTime;
             if (countdown <= 0.0f)
             {
                 GetComponent<Renderer>().material.color = Color.red;
             }
            
         }
     }
 
     void OnTriggerExit (Collider other)
     {
         Color team = GetComponent<Renderer>().material.color;
         if (team == Color.red)
         {
             countdown = 5f;
         }
         else
         {
             countdown = 5f;
             GetComponent<Renderer>().material.color = Color.blue;
         }
     }
 }

 

I appreciate any and all feedback and would love to know what I am doing wrong with my approach. One thing to note is that I have this script attached to both the object being captured and the GUI element being instantiated. Is this the right approach?

one more thought, is it having issues because the instantiated object has not detected an OnTriggerStay event so therefore it does not count up?

Thank you!

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

0 Replies

· Add your reply
  • Sort: 

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

131 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

Related Questions

Locate _MainTex from a public shader and assign to instantiated prefab for GUI 1 Answer

Instantiate PreFab on trigger location 0 Answers

Unity3d 4.6 javascript prefab with script 1 Answer

Problem with custom bolts in Space Shooter 1 Answer

Spawn power ups after random amount of time 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