• 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 /
  • Help Room /
avatar image
0
Question by jimmmmmy97 · Feb 14 at 10:00 AM · raycastgetcomponent

Unity: when I use raycast, getcomponet can not work under the if statement. I think the logic is reasonable. I do not know how to collect it...

I am a beginner about unity and C#. I need to make a planting game as my university project.


Right now, there are several objects, which are 1. a terrain model, 2. a hp bar, 3. some transparent cubes in one large empty object.


Cubes are above terrain model and play the role of grid. What I want is that when the player click those transparent cubes whose tags are "Land", trees are planted at the location of the cube and the clicked cube is destroyed. At the same time, the trees can be planted successfully only if the tag of that terrain model is "Plantable". Each successful planting action will decrease the hp of that land. Once the hp becomes 0, the tag of terrain model will become "Unplantable" and it can not be planted any more.


So, I mainly have two scripts. One is about raycast and the other one is about hp bar. However, my raycast script can not get the component of hp bar's script successfully.


My raycast script is

 void Update()
 {
     if (!EventSystem.current.IsPointerOverGameObject())
     {
         if (Input.GetMouseButtonUp(0))
         {
             RaycastHit hit;
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 if (hit.collider.gameObject.tag == "Land")
                 {
                     if (GetComponent<HpBar2>().Farmland.tag == "Plantable")
                     {
                         objectHit = hit.transform;
                         Instantiate(Tree1, objectHit.transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
                         Destroy(hit.transform.gameObject);
                         GetComponentInChildren<HpBar2>().DecreaseHP();
                     }

                     if (GetComponent<HpBar2>().Farmland.tag == "unPlantable")
                     {
                         Warning.SetActive(true);
                     }
                 }

and my hp bar's script is

 void Start()
 {
     currentHP = maxHP;
 }

 void Update()
 {
     HealthSilder.value = currentHP / maxHP;
     if (currentHP <= 30f)
     {
         Farmland.tag = "unPlantable";
     }
     if (currentHP > maxHP)
     {
         currentHP = maxHP;
     }
 }

 public void DecreaseHP()
 {
     currentHP -= 30f;
 }

I do not know what is the wrong about these scripts and they almost kill me...

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

231 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 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

Avoid using GetComponent to check if RayCast hit is interactable 0 Answers

2 same Scripts w/ GetComponent : one works, one doesn't 2 Answers

Is there a better way to check if a player is looking at a certain object? 0 Answers

Get component from parent of children hittet with raycast 1 Answer

How to change the enum value of an instance of a prefab being clicked 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