- Home /
GameObject keeps acting like gameObject.
Hello, I am having an issue where GameObject is acting like gameObject would; where the game doesn't recognize the GameObject put into the slot in the unity inspector. It instead keeps recognizing it whenever I GetComponent as a gameObject or the object that the script is currently on. Not sure what is going on or how to fix it, so any help is appreciated.
public class Scp_ConstantValues : MonoBehaviour
{
public GameObject Color1;
public GameObject Color2;
public Color Red = new Color(1.0F, 0.3F, 0.4F);
public Color Green = new Color(0.2F, 1.0F, 0.4F);
public Color Blue = new Color(0.2F, 0.3F, 1.0F);
public void SetColor1Red()
{
RocketColor1 = "Red";
ParticleSystem Color1 = GetComponent<ParticleSystem>();
Color1.startColor = Red;
}
public void SetColor1Green()
{
RocketColor1 = "Green";
}
Answer by UnityCoach · Jul 28, 2017 at 11:55 PM
There are inconsistencies between Color1, the GameObject member of the class, and Color1, the ParticleSystem within the SetColor1Red method.
By the way, I'm not sure why you have individual methods. You could have a more generic method SetColor (int colorIndex, Color color) {}
$$anonymous$$ostly because I am totally new to Program$$anonymous$$g and just go off of the help of the unity page and trial and error currently, so I'm not sure what you mean for inconsistencies. could you explain a bit more, please?
Color1, within the scope of SetColor1Red method, is declared as a ParticleSystem type. At the root of the class ($$anonymous$$onoBehaviour Script), it's declared as a GameObject.
So, when you access the startColor property or Color1, it's the ParticleSystem property. The one found on the same game object, as you assigned it with GetComponent. GameObject doesn't have a startColor property, so the compiler knows which "Color1". But if it did, the compiler would be lost.
Ooh I see what you mean, than maybe its the way I'm thinking it should go, I was trying to access another game object that is known in this script as Color1, then I am trying to access the particle system of that object, then trying to access the startColor of the particlesystem and setting it to the color of red, green, or blue. I'm assu$$anonymous$$g I have done this totally wrong then?