- Home /
How to modify child objects with parent script?
In my FPS game, I'm trying to make weapons cease rendering when the player isn't using weapon[weaponNum]. Though I don't have an actual array, I have an empty game object with a script on it that checks each frame whether or not the player is trying to switch weapons. When the player selects a weapon, I want the previous weapon to dissapear/become invisible, and for the weapon of weaponNum to become visible.
How would I reference those gameObjects in code? For example, if the player presses 1, I want their currently equipped weapon to dissapear, and the weapon in slot 1 to reappear. I'm using weaponNum for a script called useWeaps(int fireType) as well with a switch statement that fires a different projectiles depending on what weaponNum is.
void weaponSelect()
{
//change weapon model, and equipment number. use different function for firing, reloading, etc depending on which number is selected.
if (Input.GetKey(KeyCode.Alpha1))
{
weaponNum = 1;
//deactivateCurrent() activateNum() model change
}
else if (Input.GetKey(KeyCode.Alpha2))
{
weaponNum = 2;
//deactivateCurrent() activateNum() model change
}
}
To illustrate what I mean, here's an image. I would want gun1 to become invisible when the user has gun2 equipped, the opposite with gun1 equipped.
The script is on the weaponModels Parent, so how does one reference and control the child objects within that parent?
Answer by highpockets · Apr 02, 2019 at 09:15 PM
The script on the parent can reference other game objects by doing the following:
public GameObject gunOne;
public GameObject gunTwo;
Now in the inspector, you can drag gun1 and gun2 into the respective fields on the script/component. If you prefer to keep them private for whatever reason, you can do the following:
[SerializedField] GameObject gunOne;
[SerializedField] GameObject gunTwo;
The above keeps the fields visible in the inspector so you can add the game objects my dragging and dropping, but now they are private. You can also assign them from code:
GameObject gunOne;
GameObject gunTwo;
void Start(){
gunOne = GameObject.Find(“gun1”); //you have to have the exact name as the parameter/string.
gunTwo = GameObject.Find(“gun2”);
}
Now since you are using the parent script to reference them, you can activate or deactivate them or move the one you don’t want off screen somewhere. To activate/deactivate:
gunOne.SetActive(false);
gunTwo.SetActive(true);
Or move the one you don’t want to another position:
gunOne.transform.position = new Vector3(5000,5000,5000);
If you move it off screen somewhere and it remains a child, it will continue to be active and will respond to fire for example unless you set a bool or have states to know if it is in use or not.
Hope that helps, cheers
I've been trying to avoid anything public variable wise just to keep in practice though if this is the only way it'll work, I just don't like working with strings and memory in this way with my experience in C++. A many times it caused issues using strings and public stuff like that.
Also, doesn't .Find have to search every single game object in the scene? That seems extremely inefficient; even if it only has to run once that is still a LOT of stuff to look through.
Use [SerializedField]. That keeps it private and makes it available in the inspector. Yes, Find() is not super efficient, but if you just have to run it once at the start of the game, not too big of a deal. Not sure how many objects you have though.
I forgot to mention that you can use tags as well. You can tag the object in the inspector (“guns” for example). And then use FindGameObjectsWithTag() or you can look up object of type as well with FindObjectsOfType().. Lots of ways to skin a cat. Hopefully that helps. If you think I’ve answered the question, please mark it as answered though.
Your answer
Follow this Question
Related Questions
Moving certain sprites in my animation causes weird slingshot effect 0 Answers
Finding an objects children when they don't have any scripts attached? 1 Answer
Modifying HingJoint2D in a child GameObject 0 Answers
How to create a child object of an object with a specific tag? 1 Answer
Extremely confused with parent and child object system 1 Answer