var newPiece = (GameObject)Object.Instantiate ((UnityEngine.Object)GamePiece);
var newBall = newPiece.GetComponent();
newBall.PieceColor = pieceColors[randci];
And this is part of the script that is attached to Ball:
publicParticleSystem PieceEmitter { get; set; }
publicColor32 PieceColor {
get { return GetComponent ().material.color; }
set {
GetComponent ().material.color = value;
if (PieceEmitter == null) {
var pe = transform.Find(“PieceEmitter”);
PieceEmitter = pe.GetComponent();
}
PieceEmitter.startColor = value;
}
}
The transform.Find(“PieceEmitter") is returning null. Why is it not returning the transform of the PieceEmitter child of the Ball’s transform? What am I doing wrong here?
If your issue is with transform.find, then all that code should be irrelevant. Is Piece Emitter a direct child, or is it a child of a child?
Transform.find is not recursive, but you can use this method instead
Click for code
public static class ExtTransform
{
//Even though it says FindChild, it will check the parent as well :/
public static Transform FindChildNamed(this Transform transform, string name)
{
if(transform.name == name) return transform;
for(int i = 0; i < transform.childCount; i++)
{
Transform foundChild = transform.GetChild(i).FindChildNamed(name);
if(foundChild != null) return foundChild;
}
return null;
}
}
So instead of transform.Find(“name”) you use transform.FindChildNamed(“name”)
If your prefab is not instantiated yet, then I am not sure if transform.find or the method above would work.
Thank you, HiddenMonk. I think the issue is that transform.childcount is 0. Is there something wrong with the way I’ve instantiated the prefab, Ball? It looks like the PieceEmitter child did not get instantiated with the Ball object. This is my first game using Unity.
HiddenMonk, thank you for asking for the screenshot of my prefab setup. When I went to grab it, I noticed that the prefab did not have the PieceEmitter particle system. I must have added it to the object and forgotten to update the prefab! Beginner’s mistake. Thank you again for your help.