• 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 jorgon · Oct 04, 2011 at 06:02 PM · parentchildglobal variable

How can I instanciate an object when a global variable is modified?

Hi!

How can I instanciate an object (an empty object with its child objects) when a value of a global variable is reached in Javascript?.

The idea is that this object has an animation as a component object and I want instanciate this object during a short time and after the animation is watched, destroy the instanciated object.

Maybe with coroutines or there's any other easier and/or direct way to implement this?

Thank you very much.

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
Best Answer

Answer by jorgon · Oct 05, 2011 at 08:31 AM

"mensaje" is the GameObject I want to instanciate. "posicion mensaje" is the empty GameObject used as a position where I want to instantiate my "mensaje" GameObject.

I'm checking 24 times per second if activate is activated and when it's activate I instantiate the object, my "mensaje" GamObject in the desired position.

Well, I can't print any of my messages "bien" or "mal".

I've created first script "global_counter_script" the global variables used in the other two scripts which modify them:

  static var globos_azules_explotados:int=0 ;
  static var globos_rojos_explotados:int=0 ;
  static var globos_amarillos_explotados:int=0 ;
  
  static var goal_azules: boolean;

And in the second script I've modified (incremented) the value of some global variables

  private var globos_rojos_explotados:int ;
  private var globos_amarillos_explotados:int ;
  private var globos_azules_explotados:int ;
  
  private var activate:boolean;

. . .

 //after some instructions:
 if (tipo_globo =="globo_normal_azul(Clone)") 
     {
     print("entro azul");
     Contador_global.globos_azules_explotados++; //acedemos a la variable global que cuenta el número de globos azules explotados
     if (Contador_global.globos_azules_explotados==1)
         {
         print("llegamos_a uno");
         activate=true;
         }
     }

I can print my messages in this, from second script using the global variables as you can watch, but I can't evaluate the global variable in the third script as I've done in the second one.

And this is the script where I want to check the condition based in a global variable created in the first script.

 var mensaje:GameObject;
 var posicion_mensaje: Transform;
 private var activate:boolean=false;
 
 
 function update() 

{

  if (activate==true)
     {
     print("bien");
     var go=Instantiate(mensaje, transform.position, transform.rotation);
     }
     else
     {
     print("mal");
     }
 }

Thank you very much in advance.

Comment
Add comment · Show 6 · 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 syclamoth · Oct 05, 2011 at 08:32 AM 0
Share

Remember when I told you not to do that?

avatar image syclamoth · Oct 05, 2011 at 08:34 AM 0
Share

Ok, I don't see you incrementing any values anywhere. Are you sure your code does what you think it does? It probably doesn't help that nowhere in your second script do you actually set activate to 'true'. Also, update needs to be Spelled With A Capital U!

avatar image jorgon · Oct 05, 2011 at 08:40 AM 0
Share

The CAPITAL "U" in Update...Oh $$anonymous$$y God...I didn't realise!!...I'm gonna make some checkings...At least I can print "mal" in the script wasn't working well. What I don't know is why Unity didn't tell me anything...AARRGGHH...These are things of being a newbye in Unity3D...But, let give me some $$anonymous$$utes to tell you news.

avatar image jorgon · Oct 05, 2011 at 08:50 AM 0
Share

Ok. You've got all the reason. It still doesn't work due to what you told me : "activate" is never set to "true" because I changed the condition in my third script tu "false" and my object was instanciated continuosly . :-) . So I've got to check why I can't pass perfectly the value of "activate" from the second script to the third one. Another thing will be to set activate to false after the setting of activate to false. Still doesn't work but one of my problems was the CAPITAL "U" of Update...AARRGGHH...How many days spent with this stupid detail...But still doesn't completely work...

avatar image jorgon · Oct 05, 2011 at 10:09 AM 0
Share

It works perfectly: I've added some instructions for resetting the global counter and destroy my instance after a few seconds (the seconds of the duration of my animation associated to my instance). Now I only have to achieve that my instance goes with me meanwhile I'm moving...But...THAN$$anonymous$$ YOU!!. THAN$$anonymous$$ YOU!!. :-) . After some days of hitting my head aginst a wall...Haaahahaha...Now I'm happy!. It's surprising how Unity and the editor recognizes "update" and "Update" coloring the font with blue tint and additionally doesn't tell me any warning...

Show more comments
avatar image
0

Answer by Ludiares.du · Oct 04, 2011 at 06:41 PM

 if(globalvar != lastglobalvarvalue)
 {
      var go = Instantiate(prefab, transform.position, transform.rotation);
     var animationlenght = go.animation.clip.lenght;
     Destroy(go, animationlenght);
 
 }
 lastglobalvarvalue = globalvar;
Comment
Add comment · Show 4 · 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 jorgon · Oct 04, 2011 at 08:52 PM 0
Share

$$anonymous$$ore or less this is what I've been trying but: does this must be included inside an update(){} or a start(){} function?.

And one more thing: how do you use "globalvar" when you evaluate the condition?. if(name_of_script_which_contains_the_global_var.globalvar!=lastglobalvarvalue)?. I wanna say :"may I use this way to compare the value of the globalvar (with the dot in the middle)?. I've introduced a print("") to check if I entry inside my if but I can't execute anything inside it. I think that my problem is the syntaxis.

Thank you very much.

avatar image syclamoth · Oct 05, 2011 at 07:54 AM 0
Share

you put in the Update loop, but make sure it only happens when you want it to- you may want to put something in there which resets the global value after doing your animation stuffs.

avatar image jorgon · Oct 05, 2011 at 08:05 AM 0
Share

I'm gonna paste my code because I can't print anything from inside my if...I don't know what's happening. It means that I can't enter in my "if".

avatar image syclamoth · Oct 05, 2011 at 08:06 AM 0
Share

Well, the if will never execute if the condition never evaluates true! $$anonymous$$ake sure you have set up the conditions properly. Also, when you post your code, please update your question- don't post it as a comment or answer.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Any help getting my trigger to fire? 0 Answers

How to do parenting with a clone gameObject? 1 Answer

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Get Parent name of child 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