M
M
MagicalPrincessFromAnothe2019-01-17 13:50:49
C++ / C#
MagicalPrincessFromAnothe, 2019-01-17 13:50:49

How to get the name of an object through its component?

Hello everyone, when developing the game, the following question arose: I have a cell of the earth, and I can dig it. I implemented the digging process in this way: when you press the "dig button", the prefab (there are 10 of them) is replaced by the cell on which the "player's sensors" are now located. But it turned out that I have a lot of such cells on the level, and the prefabs are called the same (each cell of the earth has the same "digging process"), and when you press the dig key, all the cells on the level, and not just the one on which are my character's sensors. I made a script for one of the sensors, which should determine which cell the player is currently looking at and only dig it. Here is the code:

hit = Physics2D.Raycast(this.transform.position, Vector2.down, 1.0f, groundLayer);
        if(hit) {
            obj = (hit.transform.name).ToString();
            Debug.Log(obj);
        }

But this construction returns me the name of the current prefab, and not the name of the cell object on the stage (the cell objects I have have a unique name like "cell1" and so on). Question: how can I remake the above code so that it returns the name of the object on the stage to which the prefab is attached, which detects raycast ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
p4p, 2019-01-17
@MagicalPrincessFromAnothe

Add a trigger and a bool thisCellActive variable to the cells and set it to true when the player enters the zone of this cell. Respectively false when exits. And when you dig Check if(thisCellActive)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question