Hello!
I am getting an annoying bug and I can’t find a way to fix it, Cannot cast from source type to destination type. I think the problem is located somewhere in the following code. If anyone knows how to fix it that would be great, thanks!
private void OnTriggerEnter(Collider LasarCollider) {
Collider LaserCollider = gameObject.GetComponent();
if(LaserCollider.transform.parent.tag == “Asteroid”) {
print(“hi”);
}
}
----> IN A DIFFERENT SCRIPT
if (Input.GetKey(KeyCode.Space)) {
GameObject Shoot = Instantiate(LasarBeam, transform.position, transform.rotation).gameObject;
}
in your OnTriggerEnter, i am confused as to the purpose of the line Collider LaserCollider = gameObject.GetComponent();
If you are trying to see if you collided with an object with a parent tag of Asteroid you shoudl just dom something like this…
private void OnTriggerEnter(Collider LasarCollider) {
if(LasarCollider.transform.parent.tag == "Asteroid")
print("hi");
}
Which line is actually throwing the error? I am assuming it is the line I mentioned wasn’t needed, as you are asking it to get a component, but not specifying what type of component to get.
Hopefully this helps,
-Larry
The line throwing the error was the 2nd part of the code I posted above. Thanks for your advice, I’ll see if it works.