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 /
avatar image
0
Question by thomasotje10 · Jun 18, 2018 at 08:45 PM · particlesystemsimplesyntax

The difference between these lines?

Can somebody tell me why this works:

         var ps = dustTrail.GetComponent < ParticleSystem>();
         var mains = ps.main;
         mains.startColor = Color.blue;

but this doesn't?:

         var dustColor = dustTrail.GetComponent<ParticleSystem>().main.startColor;
         dustColor = Color.blue;

It's not much of a problem but i do feels as if it's something i should know.

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
1

Answer by Reynarz · Jun 18, 2018 at 09:14 PM

Its simple:, structs are value type, not reference type; this means that if you edit an struct the changes will be locally (stack memory), not in the heap.


A class is diferent (ParticleSystem is a class), is a reference type (heap memory). In your first example you are modifying the property color of the particleSystem object. in your second example you are modifying a local struct of type color but the ParticleSystem object will not be notifyed because dustColor is not part of the ParticleSystem object.


I recommend that you read this :) https://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net

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 SlowCircuit · Jun 18, 2018 at 08:49 PM

In the first one (the one that works) you're changing startColor. In the second one (that doesn't work) you're changing startColor.color. I'm going to assume the latter variable either doesn't exist or doesn't do what you want.

[EDIT: Their original unedited post had them setting dustColor.color, but they edited it after my answer.]

Comment
Add comment · Show 2 · 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 thomasotje10 · Jun 18, 2018 at 08:52 PM 0
Share

I actually looked right over that little difference so thank you for telling me, although i just removed the "color" variable and i still get the same result :/

avatar image SlowCircuit thomasotje10 · Jun 18, 2018 at 09:11 PM 0
Share

That's cause now the second thing is just changing a color to a different color. dustColor is just not a reference, but a copy of the startColor you're setting it to. So when you change it, you're just changing the copy. The top way you're doing it is the more proper way.

How I'd do it:

 var main = dustTrail.GetComponent < ParticleSystem>().main;
 main.startColor = Color.blue;

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

87 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

Related Questions

Problem using point.Length in a particle system c# 2 Answers

How to convert new unity input system input actions to booleans 1 Answer

Particle distribution on mesh: How to implement a density map? 0 Answers

How do I set the position of a newly emitted particle in a local enabled particleSystem? 1 Answer

Unityscript - Using "as" to cast for GetComponentsInChildren returns empty array 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