I'm a noob at this. There is a problem that I could not solve alone, and I want you to help me, it turns out that every time I enter the unity playmode, the parameters of a script disappear. I don't know if it's because of my script or it's something else. Anyway I leave you screenshots of my project in unity and the problematic script. Thank you very much in advance.
When I press the "Atacar" button, I get the error below.
Your issue is in Start()
you are trying to get the component GameObject, which does not exist. You already have the GameObject references and GameObjects do not have components GameObjects. A Component is a piece of a GameObject but a GameObject can not have GameObject Component.
If you remove your current Start()
function the object should not disappear when you hit Play.
You only need to use the GetComponent method when you are trying to get a Component on an object you have a reference to. For instance, if you wanted to get a reference to the Sprite Render
on your gameobject and did not want to make a public variable, you can get that reference by using GetComponent<SpriteRenderer>()
.
If you are still confused about the overall concept of the Component to GameObject relationship, you can check out the official docs or another useful Unity post.