- Home /
Is there a good way to change a variable from a child object of the object that collided with a trigger, without giving a reference beforehand?
My 2D Player Character is constrcted like this, with the ,,Player" Parent holding a script with the Player's Stats and the ,,Player Sprite" Child holding a Collider and the Sprite. I chose to make the Child hold the Collider and Sprite, instead of also putting that in to the Parent, because it makes the Player much easier to animate like this.
But now, I need a completely unrelated Prefab GameObject to change a variable on the ,,Player" Parent, after it has collided with the collider of the ,,Player Sprite" Child.
This unrelated GameObject has a 2D Trigger Collider, and I want it to do something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CertainObject : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
//Change the public bool ,,myBool" of the ,,Player" GameObject to true
{
}
}
But it was very annoying to have to give a reference to the Player.cs Script, every Time I create a new instance of this GameObject, and I would not want to do this again, unless I have to.
Answer by TheCrazzyFace · Aug 27, 2019 at 04:10 PM
To access the script you need to use playerScript = playerObject.GetComponent<Player>()
instead of playerScript = playerObject.Player
Answer by Phlegma · Aug 27, 2019 at 05:21 PM
Adding to TheCrazzyFace's answer:
If you only need playerScript
and not playerObject
, you can use playerScript = other.transform.GetComponentInParent<Player>()
Thanks very much to you and @TheCrazzyFace I don't know if I should click accept on your answer of @TheCrazzyFace 's answer. @TheCrazzyFace gave the original answer and you added to it by showing me how to get the Script reference without the Player refence.
Without your answer, getting the Player Script reference automatically in my game would still work perfectly thanks to @TheCrazzyFace 's answer, but not without first getting the Player Object refernce first, which I would not have realized is unnecessary.
I think it makes much more sense to click accept on @TheCrazzyFace 's answer.
Answer by PKU_DZY · Aug 27, 2019 at 12:58 PM
use "transform.parent.gameObject" to access parent object of a child game object. In your example, use "other.transform.parent.gameObject" would help.
I think I'm about to get there, but I think I still need a little bit more help: I have made this Script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CertainObject : $$anonymous$$onoBehaviour
{
private GameObject playerObject = null;
private Player playerScript = null;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
if (playerScript == null)
{
playerObject = other.transform.parent.gameObject;
playerScript = playerObject.Player;
}
playerScript.myBool = true;
Debug.Log("Collider");
}
}
}
The only problem is, that I don't know how to get around this error which is caused by the Line playerScript = playerObject.Player;
: Assets\Scripts\CertainObject.cs(16,33): error CS1061: 'GameObject' does not contain a definition for 'Player' and no accessible extension method 'Player' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
EDIT: @TheCrazzyFace has published the exact answer I needed.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get my child objects to follow the parent? Also, not sure if this is working. 0 Answers
Find GameObject in another loaded scene 2 Answers
How to call OnTriggerEnter2D 4 Answers