I am at the halfway point of the Realm Rush chapter of the 3D course and have a few questions about correctly adressing components and their properties.
Let me start with an example from Realm Rush
[SerializeField] ParticleSystem projectiles;
void Attack(bool isActive)
{
var shooting = projectiles.emission;
shooting.enabled = isActive;
}
Why do we have to take the extra step of introducing the variable “shooting”?
Why does “projectiles.emission.enabled = isActive” not work in the Attack Method?
I assume the reason behind this is the same but I have another example.
When building a tower in Realm rush, I would like the material of the floor tile to change, so I came up with this Method for the Tower script:
Transform parent;
Renderer ground;
[SerializeField] Material towerMaterial;
void ChangeGroundColor()
{
ground = parent.Find("Tile").GetComponent<Renderer>();
ground.material = towerMaterial;
}
This does work, but why can’t I just address the material directly with the following?
Transform parent;
Renderer ground;
[SerializeField] Material towerMaterial;
Material groundMaterial;
void ChangeGroundColor()
{
groundMaterial = parent.Find("Tile").GetComponent<Renderer>().material;
groundMaterial = towerMaterial;
}
Thank you in advance