Hi, I’m still pretty new to C# and still trying to understand using GetComponent(). I understand I can use GetComponent() like this when the script is attached to just one game object:
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>(); // rb = RigidBody component of object
}
And can then make any changes to rb and have it reflected in the game. However, if I have one script attached to multiple game objects, but only want to affect one of them with a specific method or function, how would I do this? I’ve seen people use something like gameObject.GetComponent(), but I am not sure if “gameObject” is referring to the object name or something else like a tag (which would imply separate tags for everything that needs to be manipulated by itself), and how I would initialize this at the start of my script so it is accessible.
I also have another question that I’m curious about - say I have an image that I change the colour of in a script between black and white under certain conditions. Is there a way to use the current colour of the image in a boolean statement - for example, something like this:
image.color = Color.white;
if (image.color == Color.white)
{
image.color = Color.black;
}
Or would I be better off just creating a string/int that corresponds to the image’s current colour and using this in the boolean statement instead? This obviously isn’t my intended usage, I’d just like a way to run some code within void Update() that changes the colour of an image, but I don’t want to attempt to overwrite the image colour if it is already the colour I want it to be.