简体   繁体   中英

Change particle system material Unity 3D script

I am trying to create particles emission for my objects which are actually pawns in a chess game. I changed particles material using Inspector for particle system and it works fine, however, when I try to change it with a script, nothing happens (particles stay pink for the black pawn).

I am not sure what is wrong with the script, whether it's a change of material problem or maybe I need to set the path for material. I would be glad if someone suggested a solution!

Here is the script:

public class BlackPawnParticleSystem : MonoBehaviour 
{
    private Material m_Material;
    private ParticleSystemRenderer psr;

    private void Start() 
    {
        var ps     = GetComponent<ParticleSystem>();
        m_Material = GetComponent<Renderer>).material;
        psr        = GetComponent<ParticleSystemRenderer>);

        psr.material = Resources.GetBuiltinResource<Material>("BlackBishop.mat");
    }

This is how it looks like:

在此处输入图片说明

EDIT:

Changes in code:

public class BlackPawnParticleSystem : MonoBehaviour 
{
    private Material m_Material;
    private ParticleSystemRenderer psr;

    private void Start() 
    {
        var ps       = GetComponent<ParticleSystem>();
        m_Material   = GetComponent<Renderer>().material;
        psr          = GetComponent<ParticleSystemRenderer>();
        psr.material = Resources.Load<Material>("BlackBishop");
    }

Location of my materials: 在此处输入图片说明

Inspector for the material:

在此处输入图片说明

Script attached to the object:

在此处输入图片说明

The problem is this line: Resources.GetBuiltinResource<Material>("BlackBishop.mat")

It is returning null. The Resources.GetBuiltinResource function is used to load resources that are built-in into Unity not ones created by you. For example, "Sprites-Default.mat" . There is no build-in material named "BlackBishop.mat" so it returns null.


To load material you created, use rhe Resources.Load function.

First, create a folder called "Resources" then put your "BlackBishop.mat" material there. You can now load it as below:

Resources.Load<Material>("BlackBishop");

Notice how I did not include ".mat" in the name. You don't include the extension name when using the Resources.Load function. If you do, it will not find it.

EDIT :

But still nothing happens on my game scene

Please carefully:

1 .Like I said in my original answer, you must create a folder named "Resources" and must put the "Black Bishop" material inside that folder. You have not done this and it should not work. The spellings of thisfolder must be correct so copy it directly from this answer. Again, it should be named "Resources" .

2 .Your material is named "Black Bishop" not "BlackBishop" . See the space. Please fix this in the code. That should be Resources.Load<Material>("Black Bishop"); not Resources.Load<Material>("BlackBishop"); .

3 .Your material is currently using the "Standard" shader. Change that to Particle shader. You can do that by selecting the Material and changing the shader from "Standard" to "Particles/Alpha Blended Premultiply" . That's it. Your problem should now be solved.

Extra:

I noticed you are using folders to organize your resources. Your "Black Bishop.mat" is inside Arcane Cyber --> Chess --> Materials --> Pieces --> Plastic. I suggest you move the "Materials" folder to the "Resources" folder created in #1 above.

Now, your "Black Bishop.mat" should be inside Assets --> Resources --> Materials --> Pieces --> Plastic.

You can now load it as:

Resources.Load<Material>("Materials/Pieces/Plastic/Black Bishop");

If you still have problems, please update your code and new Inspector images again. Remember that spellings count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM