• 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 lampshade · Jun 25, 2010 at 05:37 PM · gameobjectsforeach

foreach GameObject condition error

Hi,

When the player instantiates a particlePrefab, particles emit at the players position. What I need to do is for when the player moves, I need the particles to move with the player, in the direction the player is going, and also to gradually destroy several of the particles (not all of them at once).

Essentially, the player runs up to a trigger, player instantiates particles, player moves, and the particles destroy immediately (not what I want). I need to gradually destroy particles in the direction the player moves in...for now, I am working on gradually destroy particles (fading them out) while holder[4].a is > 0.

//In seperate script file public static GameObject instantiated ;

instantiated = (GameObject) Instantiate(particlePrefab, transform.position, Quaternion.identity); //End seperate script file //Call our coroutine to gradually destroy particles

public static IEnumerator ResetParticles() {

 resetting = true;
 holder = new Color[5];
 holder[4].a = 1;


 while (holder[4].a > 0) 
 {
     foreach (ParticleAnimator m_emitter in emitters) 
     {
         if (m_emitter) 
         {                   
             holder = m_emitter.colorAnimation;
             holder[4].a -= .005f;
             m_emitter.colorAnimation = holder;
         }
     }
 }
 foreach (ParticleAnimator m_emitter in emitters) 
 {
     if (m_emitter) 
     {
         m_emitter.particleEmitter.emit = false;
         m_emitter.particleEmitter.ClearParticles();
     }
 }
     resetting = false;
     sent = false;

 yield return 0;

}

What the above does, without a PreFab instantiation is destroy's particles gradually. I need to be able to use my instantiated GameObject varaible because it has the instantiated particle prefab.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by qJake · Jun 25, 2010 at 06:51 PM

Note: This answer was posted before the asker decided to completely rewrite their question, adding more details. This answer may no longer apply.

What are you trying to accomplish here? Perhaps if you actually explained your goal, we may be able to help better (instead of just posting code and saying "this doesn't work, help me").

Regardless, the problem is that you're trying to iterate through a variable that isn't a collection. A GameObject isn't an array or a collection, there isn't "more than one of them", so you can't use a foreach on it. I think you just want to do this:

PlayerDidStuff.instantiated.particleEmitter.emit = true;
Comment
Add comment · Show 3 · 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 lampshade · Jun 25, 2010 at 07:54 PM 0
Share

$$anonymous$$y Goal is to make particles flow around the player in the direction that the player moved, using eulerAngles. I instantiated my preFab and I am trying to control the movement of the particles according to the direction that the player moves.

avatar image lampshade · Jun 25, 2010 at 08:08 PM 0
Share

Thanks, I reposted with more code.

avatar image lampshade · Jun 25, 2010 at 08:39 PM 0
Share

I tried to fade out each element in the Color array for holder (collection) and still nothing..I need to use my instantiated varaible somehow but their is no defintion of Color or ParticleAnimator for UnityEngine.GameObject

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

No one has followed this question yet.

Related Questions

How to find all similar elements in a list? 1 Answer

Link GameObject via foreach 2 Answers

How To Select Single Object From All Objects We Found Whit Tag? 1 Answer

Instantiating Too Many Objects 1 Answer

Find gameobjects by tag, and destroy if in the distance of a certain game object... 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