Search Unity

Unable to change particle prefab color in script.

Discussion in 'Prefabs' started by TheMonkeysaur, Aug 26, 2019.

  1. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    46
    Hi, I cannot seem to change the color of my particle systems with my script.

    I am using a pooling system, but for simplicity while testing to figure this out I have just instantiated them.

    I am convinced this has to do with how I am using prefabs, the color always follows the settings of the prefab, and never changes when I ask it to through a script.

    Here is what I am doing on collision2D:

    Code (CSharp):
    1. GameObject spawnedParticle = Instantiate(tempPartPrefab, transform.position, Quaternion.identity);
    2.  
    3.             ////////////////////////////////////////////////////////////////////////////
    4.             ParticleSystem splatPartSys = spawnedParticle.GetComponent<ParticleSystem>();
    5.             ParticleSystem.MinMaxGradient splatPartSysCol = splatPartSys.main.startColor;
    6.             splatPartSysCol = new ParticleSystem.MinMaxGradient(m_spriteRenderer.color, m_spriteRenderer.color);
    If anyone knows what I am doing wrong, or why my prefabs might not adhere to the changes I try to make, please let me know.

    Thanks.


    EDIT:

    I clearly am using prefabs incorrectly. I just created two sprites for debugging. Both in scene but one is a prefab and the other is not.

    Code (CSharp):
    1. tempColorDebugInScene.GetComponent<SpriteRenderer>().color = m_spriteRenderer.color;
    2.         tempColorDebugAsPrefab.GetComponent<SpriteRenderer>().color = m_spriteRenderer.color;
    One works (no prefab) and the other (prefab) doesn't.

    If anyone can tell me what I'm not getting here that would be really helpful.

    Thanks.
     
    Last edited: Aug 26, 2019
  2. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
    Prefabs don't exist at runtime (and in Play Mode); they get converted to regular GameObjects, so it seems quite unlikely that the difference comes down to one object being a Prefab and the other not.

    It's not clear from your code snippet where tempColorDebugAsPrefab is coming from, but I'd double check all assumptions. Are you modifying the prefab instance and not accidentally modifying the Prefab Asset? Is the object you're inspecting and not seeing any change on actually the Prefab instance? Etc.

    If you have a minimal repro and remain convinced it comes down to one being a Prefab and the other not, we'd need a bug report with repro steps and the repro project to be able to look into it.