The code provided by the lecture is not compatible with Unity 5.5 (we have been warned about that)
Below is the code which I tried and eventually a solution which works but seems quite an hassle.
Unity 4 Code
as found in the lecture
void PuffSmoke () { GameObject smokePuff = Instantiate (smoke, transform.position, Quaternion.identity) as GameObject; smokePuff.particleSystem.startColor = gameObject.GetComponent<SpriteRenderer>().color; }
Result in Unity 5.5.1f
- Compile Error: âGameObject.particleSystemâ is obsolete: âProperty particleSystem has been deprecated. Use GetComponent() instead. (UnityUpgradable)â
- Compile Error: âComponentâ does not contain a definition for âstartColorâ and no extension method âstartColorâ accepting a first argument of type âComponentâ could be found (are you missing a using directive or an assembly reference?)
Unity 5.4 Code
as Suggested by Johnmcabe in Unity 5 updates thread and by Rob in ParticleSystem crossed over thread
void PuffSmoke() {
GameObject smokePuff = Object.Instantiate (smoke, this.transform.position, Quaternion.identity) as GameObject;
ParticleSystem ps = smokePuff.GetComponent<ParticleSystem> ();
var main = ps.main;
main.startColor = this.GetComponent<SpriteRenderer> ().color;
}
Result in Unity 5.5.1f
- Runtime Error: NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
UnityEngine.ParticleSystem+MainModule.set_startColor (MinMaxGradient value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:499)
Brick.PuffSmoke () (at Assets/Scripts/Brick.cs:73)
Brick.HandleHits () (at Assets/Scripts/Brick.cs:57)
Brick.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Brick.cs:40)
A Unity 5.5.1f solution I came up with
-
Removed the particle system under the Smoke prefab and instead add the particle system as a component to the smoke-prefab gameobject
-
Add a script to the Smoke prefab (in my case I called it ParticleSystemColorChanger)
-
Within the SmokePuff function (in brick.cs) we replace thesmokePuff.particleSystem.startColor = gameObject.GetComponent().color; with so the entire function now reads like
private void PuffSmoke() { GameObject smokePuff = Instantiate(smoke, gameObject.transform.position, Quaternion.identity); smokePuff.SendMessage ("SetColor", gameObject.GetComponent<SpriteRenderer> ().color); }
- Below is the ParticleSystemColorChanger script/class
using UnityEngine; public class ParticleSystemColorChanger : MonoBehaviour { void SetColor(object value) { if (value.GetType() != typeof(Color)) { Debug.LogError("Incorrect value has been passed to ParticleSystemColorChanger:SetColor"); return; } var ps = gameObject.GetComponent<ParticleSystem>(); if (ps == null) { Debug.LogError("ParticleSystemColorChanger script has not been attached to a GameObject with a ParticleSystem"); return; } var main = ps.main; main.startColor = (Color)value; } }