- Home /
Removing specific child objects
I have been searching for the term for multiple hours and cant find any help..
function Update(){
if(Input.GetKey(KeyCode.Q)){
child = transform.Find("Player/Item"); //find the item
child.transform.parent = null; //remove it from its parent
}
}
I don't understand what you are asking. How does this code not work? What are you trying to do?
once I press q, the object "Item" should be detached from the parent (Player)
NullRefrenceException: Object refrence not sent to an instance of an object ItemDequip.Update () (at Assets/ItemDequip.js:4)
Answer by robertbu · Nov 23, 2014 at 03:24 PM
Assuming this is on the player, you are starting too high. Line 3 should be:
transform.Find("Item");
You don't include the name of the game object that you start with in the path. For example say you had a hierarchy like:
Player
Items
Item1
Then to get to item1 starting with the transform of player, you would to:
transform.Find("Items/Item1");
aw sweet! it works, still says that error that I wrote previously but doesnt seem to do anything
If you are still getting a null reference exception, then it means it is not working. Do a Debug.Log(child) to see if the value is null. If it is null, then something is still not right about the path or the name. The name must match exactly, and 'Item' must be an immediate child of 'Player'.
If you are sure the null reference is co$$anonymous$$g from this section of this script, then look for another copy of this script on another game object where the hierarchy is not the same.
if I put the script on something else than the player it doesnt work.. hm
Your answer
Follow this Question
Related Questions
Find children by tag from Player 1 Answer
Make a simple tree 1 Answer
Find gameobject with name and if child of Player 1 Answer
Create multiple instances of an object 2 Answers