- Home /
I want ball to be invisible after colliding with goals?
So I have a game where the main idea is to get balls into goals. In one level, there are soccer goals all around. The balls need to disappear when they get in the nets. I have used a script called "Score" in C#, attached to the ball object and tried DestroyObject(gameobject) as that would make the balls disappear. However, I found it doesn't always do it, especially after you restart the level. and does not seem like people online like people destroying objects like so. For my game, these balls do not need to respawn. Once they are gone, they are gone. Just restarting the level where everything resets, but that's already done for me somewhere else. So here is what I had.
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "Goalposts" || collision.gameObject.name == "FootyGoals" ||
collision.gameObject.name == "GoalCollider" || collision.gameObject.name == "Goal ring")
{
Destroy (this.gameObject);
//I understand what gameobject.SetActive(false) could do, but it wouldn't do the similar idea of making it invisible. } }
What do you mean it isn't destroying objects when restarting the level? How are you restarting the level? how come sometimes it destroys and other times no? $$anonymous$$aybe it's not colliding that is why it's not destroying? Question is too vague to be able to help you.
Answer by Salzmann · Apr 11, 2014 at 09:39 AM
Hey, this will help you make the ball invisible - I don't know if this will help, if it doesn't, give me feedback and I will try to think of something else.
The ball has a material. You can manipulate the color of the material with code. However, in order to manipulate the alpha of the material, the shader has to be of the transparent type. Just click on the material that all your balls have and click on the drop down box, there you can choose the type "Transparent" and in there you can choose the same type your shader had been. Once that is done, you can change the alpha by accessing the material's color like so:
GetComponent<MeshRenderer>().material.color = new Color(GetComponent<MeshRenderer>().material.color.r,
GetComponent<MeshRenderer>().material.color.g,
GetComponent<MeshRenderer>().material.color.b,
GetComponent<MeshRenderer>().material.color.a);
You can set the color then - including the new alpha. The alpha is the 4th number in the Color() constructor and it is a float between 0 and 1.
It's supposed to not be touchable though. Transparency still could cause the ball to be collidable, and accidently collide with the car or other objects, causing unwarranted bugs. The ball has to disappear and be destroyed safely. $$anonymous$$aybe just better off giving a way to destroy it. Sorry. I should have explained it needs to disappear by being inactive. But I tried setActive(false) and it doesn't do it, but Destroy does, but only half the time...
In that case it is probably a bug in your code. Destroy() is reliable.
I have to send the entire thing to get anywhere don't I? Never $$anonymous$$d.
Your answer
Follow this Question
Related Questions
gameObject.name for more then one object? 1 Answer
Problems with save 1 Answer
Collision problem 2 Answers
Is CompareTag faster than checking directly the GameObject? 1 Answer