Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wanted to be able to call a method in the head script from the apple script in my snake game remake in Unity, but when I use [SerializeField] Head Head, it doesn't work and brings up an error saying that there isn't that GameObject even thought the name of the other GameObject is Head and it works when I call a different GameObject, but it doesn't work with the Head GameObject. I tried using variable type GameObject, but Head.Eat(); was still giving the error that they can't find the method even though I had the method public void Eat(){} with no errors in that script or that GameObject.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Apple : MonoBehaviour
{
    [SerializeField] Text scoreText;
    [SerializeField] Player player;
    private int Score = 0;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Destroy(gameObject);
        Score++;
        player.Grow();
        scoreText.text = Score.ToString();
    }
}

The error I got is that even though there is a Player GameObject with a script named player with a grow function in it, for some reason, Unity gives me the error AssetsScriptsApple.cs(9,30): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?) Even though the GameObject Player is right there with no changes in capitalization or anything else, and Unity still gives the error and can't recognize it. I tried to use [SerializeField] GameObject Player instead, but after using that, when I use Player.Grow, I still get an error which says that there is no such function. Edit: I tried using [System.Serializable], but it still gives the same error.

question from:https://stackoverflow.com/questions/66052679/unity-serializefield-head-head-doesnt-work-in-the-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
459 views
Welcome To Ask or Share your Answers For Others

1 Answer

First, you should format your question, with some line feeds and with code tag for exemple, to be more clear in your question. (it is very hard to read and understand what you want exactly)
Since it is not clear what you want to do, I will answer as best as I can.

Secondly, have you filled your serialized field Head in the inspector ?

You should also not use the same word as the type and the variable names, because it is ambiguous for the compiler to determine if you want to reference the type or the variable.
You can write something like this :

[SerializeField] private Head m_head = null;

m_head.Eat();

However, based on what you described, I would do it differently.
Instead of referencing your snake head in the apple (I assume that is what you are doing) I will attach a box collider set to Trigger on the snake head, and in the Head script I would do something like this :

OnTriggerEnter(Collider other)
{
    Apple apple = other.GetComponent<Apple>();
    if(apple != null)
    {
        Eat();
        Destroy(apple.gameObject);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...