Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 22, 2021 at 10:05 AM by FrostKnightTheGreat for the following reason:

I got solved it myself

avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

188 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can RaycastHit2D.point be inside a BoxCollider2D if raycast was performed from outside that collider? 1 Answer

What's wrong with my RaycastHit2D? 1 Answer

What's wrong with my RayCastHit2D. Raycast only on positive values on horizontal axis and on negative values on vertical axis 0 Answers

Raycast2D not returning any transform instead keeps returning void. 1 Answer

Raycast detects 2/3 of a collider? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges