G
G
Gigabait2020-07-23 12:22:39
C++ / C#
Gigabait, 2020-07-23 12:22:39

Implementing script communication?

I must say right away that I am new to Unity.
And my question is, how can I implement script linking in such a way that they are universal, i.e. that they work in all my projects, and not depend on one project.
I don't want to shove a bunch of scripts into one object. For example, like "movement, attack, etc. into the Player object".
I don't want to use "GetComponent" and the like.
Therefore, I drew an approximate diagram of my project as I would like it to "Communicate"
YBsZa4n.png

But I don't know how to implement it.

Here's one of the problems I recently ran into.
I have a class "PlayerClass". I hung the "PlayerLogic" script on the PlayerObject, because I no longer know how to implement it differently. The "PlayerClass" class has a "Move" method that I call in the "PlayerLogic" Update that should move the player. But he doesn't move because he doesn't know who to move. The problem is that I don't know how to get the object on which "PlayerLogic" weighs from "PlayerClass"

public class PlayerLogic : MonoBehaviour
{
    private PlayerObject player;
    
    void Start()
    {
        player.NewPlayer("BOT");
    }

    void Update()
    {
       player.Move();
    }

}


public class PlayerObject : MonoBehaviour
{
    private int health;
    private string name;
    private int speed = 2;

    public void NewPlayer(string _name)
    {
        name = _name;
    }

    public void Move()
    {
        // Move with Transform
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-07-23
@freeExec

In fact, the component model of the unit is just designed for the fact that a specific component hangs on the character KickWeaponor BlasterWeapon, and not MyBigAllWeaponTypeOfMyWorldClass. And it GetComponent<BaseWeapon>().Attack()starts the whole show after the button Fire.
Well, in your case, the problem is that it Playercame from nowhere, and was not a consequence PlayerLogic.SpawnNewPlayer(), then it would have had a link to the player. At worstObject.FindObjectOfType

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question