- Home /
Child cant apply variables from parent
I trying to pass my parent variables to my child they are passing but I'm get a NullReferenceException: Object reference not set to an instance of an object. I checked all the answers on this and could not find an answer but I might of found a clue that my parent is not getting the child of my child but i'll post the code down there i still get the same result If you could help (or direct me some one who knows how to fix this.). Help is much appreciated. (I cut out code parts that are not likely part of the problem.)
the Layered objects are: Player (Parent Script here) groundcheck imported model Material of model HitBox(The Child Script here)
And the parent communication script
HitBoxCollider hitBox;
void start()
{
hitBox = transform.GetChild(1).GetComponentInChildren<HitBoxCollider>(); // also tried this this hitBox = transform.GetComponentInChildren<HitBoxCollider>();
}
void Update
{
HitBoxData();
}
void HitBoxData()
{ /*these are giveing me a NullReferenceException: Object reference not set to an instance of an object. */
hitBox.knockBackX = knockBackX1;
hitBox.knockBackY = knockBackY1;
hitBox.knockBackLength = knockBackLength;
hitBox.damageToGive = damageToGive;
}
And the child communication script
PlayerControls pc;
void Start () {
pc = transform.parent.GetComponentInParent<PlayerControls>();
}
Answer by pankajb · May 10, 2016 at 02:27 PM
I noticed from your previous question that you are disabling hitBox in start method.
// Use this for initialization
void Start () {
pc = transform.parent.GetComponentInParent<PlayerControls>();
hitBox.SetActive(false);
}
did you change setActive
to true somewhere ?
yes It goes to true within animations but have tried leaving it on and got the same result
Okay its like 75 percent fixed the problem was when i renamed my gameobject that had the script from hitter to hitbox and found another fix where the game object has to be on all the time or the script that it is comunicating with will run into an error. now i got new problem how can i have the playercontrols ignore the script when current attack script isn't in use. the closest work around i found was making the radius as close to zero and have it controlled through animation.
okay i fixed my problem and had to redo alot of things but is now working
Answer by rober-psious · May 10, 2016 at 02:18 PM
Your function is not being called because start is not equal than Start ;)
Check line 3 of your code.
Sorry but i typed the code into the code section i forgot to capitalize the start sorry but its already capitalized in the actual code.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Adressing a Child Object after it´s unparented 0 Answers
Make child's transform independent of parent 1 Answer
Change an object's grandparent 2 Answers