Unity 5 updates

Can you .zip the project up and upload here for me so I can take a look.

Also, is your “Smoke” a prefab?

If it helps, I further worked to isolate the problem by seeing if the script could access the Transform property of the smokePuff object that gets instantiated, instead of the ParticleSystem, to see if the problem lay with the particles. The below code threw the same error:

void PuffSmoke()
{
	/* my attempt at this... kept saying no object reference */
	Debug.Log("PuffSmoke initiated.");
	GameObject smokePuff = Instantiate(benParticles, transform.position, Quaternion.identity) as GameObject;
	Debug.Log("GameObject smokePuff instantiated.");
	Transform smokePuffParticles = smokePuff.GetComponent<Transform>();
	Debug.Log("ParticleSystem smokeParticles set to smokePuff's particlesystem.");
	// ^ that line doesn't get debug.logged, therefore the line above it isn't executing!

	//smokePuffParticles.startColor = GetComponent<SpriteRenderer>().color;
	Debug.Log("smokePuffParticles startColor set to this brick's color.");
	Destroy(gameObject, myAudioSource.clip.length + 1f);
	Debug.Log("PuffSmoke completed.");
}

And to double-check, here’s my Smoke prefab (this is what’s dragged into the benParticles spot in the Brick’s inspector). Transform and ParticleSystem certainly seem to be there!

Just saw your comment - zipping in one sec.

ok… will take a look…

I think the public ParticleSystem bensParticles; needs to be public GameObject bensParticles; but will take a look and see…

Just PM’d it to you.

Grabbing…

Ok…

Resolved…

As mentioned above, change the following line in Brick.cs;

    public ParticleSystem benParticles;

to;

    public GameObject benParticles;

When you do this it is changing the type, as such, if you look in the Inspector for each of the three bricks in the Hierarchy you will see a “Type Mismatch” error for the Bens Particles variable. Fix this by dragging in the Smoke prefab again.

Run the scene.

Assuming all good, don’t forget to make the same changes to the Brick prefabs.

Yes, it works now!!! :smiley: What I don’t understand, then - why do we need to cast the smokePuff GameObject “as GameObject”, when benParticles was declared as a GameObject, and we are declaring the smokePuff as a GameObject? I tried removing that part and Unity said it was just an Object, but we declared them as GameObjects?

1 Like

Ok, so this is going to be all about types.

In this line;

GameObject smokePuff = Instantiate(benParticles, transform.position, Quaternion.identity) as GameObject;

You have declared a variable called smokePuff of type GameObject.

The Instantiate() method returns an object of type Object, so in order to use your variable you need to cast what is returned as a GameObject.

The issue with what you had was that you were declaring bensParticles as a ParticleSystem, but actually it wasn’t a ParticleSystem object that was being passed in through the Inspector, it was an object of type GameObject which had a ParticleSystem component.

Hope this makes sense :slight_smile:


See also;

1 Like

Gotcha! Yes, that clears it right up! I did some looking up to get a better understanding of casting, and what my limited grasp gathers is that casting the generic Object to a GameObject is basically getting the GameObject data from the Object, and removing the non-GameObject data. So all that’s left is the GameObject data, which fits happily into the smokePuff GameObject. My goodness, Unity just keeps amazing me at the depth within depth within depth of stuff! Thank you very much for the help! :smile:

1 Like

You are more than welcome. I am glad you can move forwards with the course again :slight_smile:

Brick.zip (950 Bytes)

Hey there,

I am using Unity 5.6.1. I have used the code below to change color within my handleHits function; however, it is still not changing the colour of my smoke. It’s the default purple. Both smoke and smokePuff are GameObjects.

public GameObject smoke;
public GameObject smokePuff;

void HandleHits()
{
    timesHit += 1;
    int maxHits;
    maxHits = HitSprites.Length + 1;
    if (timesHit >= maxHits)
    {
        breakableCount -= 1;
        
        levelManager.BrickDestroyed();
        SmokePuff();

        Destroy(gameObject);
    }
    else {
        LoadSprites();
    }
}

private void SmokePuff()
{
    GameObject smokePuff = Instantiate(smoke, gameObject.transform.position, Quaternion.identity);
    ParticleSystem.MainModule main = smokePuff.GetComponent<ParticleSystem>().main;
    main.startColor = GetComponent<SpriteRenderer>().color;
}

I had the same issue. Even manually changing the color of the particle system had no effect. The issue is the lack of a material being assigned to the particle system by default.

With the PreFab of the Particle System selected go to Inspector - Render (toward the bottom of the inspector) - Material (click the target icon toward the right) - Select, Default Particle.

This will fix the issue.

You are possibly mixing types again like above.
Also you aren’t telling Unity where to “GetComponent” from. So put “gameObject.” referring to the gameObject this script is on, so therefore the brick. may work with “this.” but i’m still a little hazy on the scope of “this.”

Where you have you “main” as a “ParticleSystem.MainModule”, you can just have it as var

here is what i have in 5.6.1f1

        GameObject smokePuff = Instantiate(Smoke, transform.position, Quaternion.identity) as GameObject;
        var spMain = smokePuff.GetComponent<ParticleSystem>().main;
        spMain.startColor = gameObject.GetComponent<SpriteRenderer>().color;

Privacy & Terms