I have two weapons M4a1 and Pistol, inside of each weapon i have the same C# script for shoot, and i need call the public void “Shoot()” with a button (for android) but if i add the Player GameObject i can´t access to the “Shoot()” void, because this C# script is attached to M4a1 GameObject, and i need that M4a1 as child of Player.
I hope you understand my english, i´m from Colombia and I don´t speak english, thank you
For future reference, please put just a very brief overview of the question in the question box, and the full issue in the description. As for your issue, assuming you are talking about trying to access M4a1 through the Player GameObject. Here is how to do it.
Option 1:
for (int i = 0; i < transform.childCount; i++)
if (transform.GetChild(i).gameObject.name == "M4a1")
transform.GetChild(i).DOTHISTHING();
You can replace the DOTHISTHING() with whatever you need it to do. Call this when you need it to something. Option 1 is highly inefficient. Do not put this code in your final build. It has to search through every object before the M4a1 which is highly inefficient and cannot proceed to the next frame until that is done.
Option 2:
Create a variable of type GameObject called M4a1
and either set it in the inspector (or if you are creating the M4a1 at runtime (when the game is running), run this code on the M4a1 object)
void Start()
{
transform.parent.M4a1 = gameObject;
}
Make sure the M4a1 object is declared like this:
public GameObject M4a1;
It is important that the M4a1 object is public. Now, whenever you want to, you can do M4a1.DOTHISTHING();.
Hope this helps