My problem is the following, I have my player, which was a capsule, it works, but inside I add my 3d model, and inside the 3d model the animator, now, the parent component “Player” that contains Rigidbody, charactercontroller, my c#, etc , can’t access the animator
Right now, Mario has an idle animation, I wanted him to walk, but I need to access this component (Inside the player).
I don’t know if this is the correct way to do it.
Make a public Animator field, drag it in, just like 100% of everything else in Unity.
Referencing variables, fields, methods (anything non-static) in other script instances (or on other GameObjects):
REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.
Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.
That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
I already tried it, and got the error:
MissingComponentException: There is no ‘Animator’ attached to the “Player” game object, but a script is trying to access it.
You probably need to add a Animator to the game object “Player”. Or your script needs to check if the component is attached before using it.
UnityEngine.Animator.SetBool (System.String name, System.Boolean value) (at <48d00b1828004564a4e4431fc5279bdf>:0)
Player_Mov.Update () (at Assets/Scripts/Player_Mov.cs:45)
I asked something because I’m trying to learn, and the only answer seems to be that he made fun of me, taking me for an idiot, and then he wasn’t even helpful.
and then when you do that, it creates a field in the inspector for your component. Then you just drag the component into the field.
Then, later on when you want to do something with it, you access it like this.
is exactly how I have it since today, In Player I have all the components (rb, charactercontroller, capsule, c#) and inside also “mario” as seen in the image, but when I drag the controller from “Mario” to “Player” I get gives that error, that the user above took me for a ride
You haven’t shown your code at all, so we can only assume. Something in your code could be dereferencing the component, or you have errant instance of this component somewhere else in your scene that’s throwing this error.
Error messages generally never lie. If something is saying it’s missing a reference, then it’s 100% true. You just need to figure out why.
Based on your public hallucination record, which I will copy here for future reference:
I’m afraid I cannot help you any further.
This is software engineering and everything I posted above has stood the test of time and DECADES of software development experience. If you haven’t used it to solve your problem, I must conclude that either a) you are not trying, or b) you are not understanding the actual steps.
Sure… in that one you are looking at. Did you inadvertently put this script in the scene more than once? It happens a LOT. That’s why I called it out in the nullref post.
what code do you want me to show you, tell me and I’ll show you, I only have 2 elements “PLAYER” WHICH CONTAINS THE ELEMENTS OF THE PHOTO (RIGIDBODY, CHARACTERCONTROLLER, ETC) AND WITHIN “MARIO” WHICH HAS THE ANIMATOR WITH THE ANIMATIONS ALREADY READY TO USE.
IN “PLAYER” WHERE IS THE C# OF THE MOVEMENT I PUT “PUBLIC ANIMATOR …” AND IN THE UNITY INPECTOR DRAG THE “MARIO” CONTROLLER
What am I doing wrong, what do I need to show so that they understand the error. (I know there is something wrong, you don’t need to send me a link of a generic error, like the other user did)
Show us the screenshot of your inspector where it shows the public field and what you have dragged into it. Based on the error it says that you dragged the Player object into it, but you are saying that you dragged the Mario object. Please show us as otherwise we are just guessing.
Line 31 is wiping out anything you dragged in. Why is line 31 present?
If you had ACTUALLY DONE the 3-step process I listed above, you would have been done half a day ago.
This is just another example of defiantly resisting the three step process and it will just wait for you until you give in, Don’t worry, the record so far is holding out for just over one week. It is the ONLY thing that will fix a NullReferenceException. THE ONLY THING!
I accept my mistake, it was a comment of frustration, I am practicing during my free time.
Now after seeing line 31, and seeing that guide, I tried removing it, and now it works, but my question now is, that Animator should not go in that case in the “Start” method?