- Home /
This question was
closed Mar 22, 2021 at 10:05 AM by
FrostKnightTheGreat for the following reason:
I got solved it myself
Question by
FrostKnightTheGreat · Mar 18, 2021 at 08:51 AM ·
raycastscript.raycasthit2d
Unable to access a script using raycast 2d
So what i am trying do is that i am trying to shoot a raycast that destroys another object. the problem is that the raycast is messed up and i dont know what to do now. pls help
public class CUT : MonoBehaviour
{
public TREE tree;
public Camera cam;
public GameObject PLAYER;
public bool EVENT;
public GameObject treeObject;
public LayerMask p_Layer;
void Update()
{
PLAYER = this.gameObject;
if (Input.GetMouseButtonDown(0))
{
Vector3 vector3 = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3 mousePos = vector3;
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(PLAYER.transform.position, mousePos2D, 50, p_Layer);
treeObject = hit.collider.transform.gameObject;
tree = treeObject.gameObject.GetComponent<TREE>();
EVENT = tree.CutEvent;
if (hit.collider != null)
{
Debug.Log("YUP");
EVENT = true;
}
if (hit.collider == null)
{
Debug.Log("NOPE");
}
}
}
}
this is the code and
public class TREE : MonoBehaviour
{
public bool iscuttable;
public string TreeType;
public int CutType;
public GameObject player;
public LayerMask P_layer;
[SerializeField] private float Circle;
public bool CutEvent = false;
GameObject ThisTree;
public void Update()
{
iscuttable = Physics2D.OverlapCircle(player.transform.position, Circle, P_layer);
if (CutEvent & iscuttable)
{
StartCoroutine(Enumerator());
}
}
IEnumerator Enumerator ()
{
yield return new WaitForSeconds(1);
ThisTree = this.gameObject;
Destroy(ThisTree);
}
}
this the code i am trying to access
Comment
Answer by toficofi · Mar 20, 2021 at 03:09 AM
It seems that you are trying to set TREE.CutEvent
to true. When you do this:
EVENT = tree.CutEvent;
You are copying the value of CutEvent to the variable EVENT. And then when you do:
EVENT = true;
You are just setting the copied variable to true - this won't reflect on the original tree.CutEvent
.
This is because primitive types (bool, int, float, string) are value-types.
You probably just want to do tree.CutEvent = true