Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Mistyisty123 · Aug 05, 2019 at 10:31 AM · instantiateraycasttaghit detection

Raycast hit info not registering on specific gameobject

Hello, I've been trying to do a tree system, it all works fine, it registers if the tag is "Tree" and works for the trees. However when the trees do the SpawnLogs() function that spawns a log, when I left click on the instantiated log object with the tag "Log" it doesn't do anything, it doesn't register at all.

Is this something to do with the instantiate part? Why isn't hitinfo registering the log the tree spawns?

Tree script that spawns the log:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Trees : MonoBehaviour
 {
     [Header("Tree Settings")]
     public float treeHealth = 100f;
     public GameObject logs;
 
     GameObject thisTree;
     GameObject player;
     Rigidbody rb;
 
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         thisTree = this.gameObject;
         rb = thisTree.GetComponent<Rigidbody>();
         rb.isKinematic = true;
     }
 
     private void Update()
     {
         if(treeHealth <= 0)
         {
             rb.isKinematic = false;
             rb.AddForce(player.transform.right * 8);
             StartCoroutine(DestroyTree());
         }
     }
 
     private IEnumerator DestroyTree()
     {
         yield return new WaitForSeconds(5);
         Destroy(thisTree);
         SpawnLogs();
     }
 
     private void SpawnLogs()
     {
         Vector3 position = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));
 
         Instantiate(logs, thisTree.transform.position + new Vector3(0, 0, 0) + position, Quaternion.Euler(90, 0, 90));
     }
 }

Axe script that gets the hitInfo:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Wooden_Axe : MonoBehaviour
 {
     float x = 87.4f;
     float y = 241.13f;
     float z = 4.5f;
 
     GameObject player;
 
     public float attackDistance = 5f;
 
     public float minDamage = 30f;
     public float maxDamage = 5f;
     public float eyeHeight;
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("MainCamera");
         transform.localPosition = new Vector3(0.55f, -0.45f, 1);
         transform.localRotation = Quaternion.Euler(x, y, z);
     }
 
     void Update()
     {
         // Raycast
         Vector3 eyePosition = new Vector3(player.transform.position.x, player.transform.position.y + eyeHeight, player.transform.position.z);
 
         Ray ray = new Ray(eyePosition, player.transform.forward);
         RaycastHit hitInfo;
 
         Debug.DrawRay(ray.origin, ray.direction * attackDistance, Color.red);
 
         if (Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast(ray, out hitInfo, attackDistance))
             {
                 if(hitInfo.collider.tag == "Tree")
                 {
                     float axeDamage = Random.Range(minDamage, maxDamage);
 
                     Trees treeScript = hitInfo.collider.gameObject.GetComponent<Trees>();
                     treeScript.treeHealth -= axeDamage;
                 }else if(hitInfo.collider.tag == "Log")
                 {
                     float axeDamage = Random.Range(minDamage, maxDamage);
 
                     Logs logScript = hitInfo.collider.gameObject.GetComponent<Logs>();
                     logScript.logHealth -= axeDamage;
                 }
             }
         }
     }
 }

Is it to do with the Instantiate in the Tree script that instantiates the log? Because the log has the tag and a box collider so I see no reason why it's not working, unless I'm being dumb and missing something...

Comment
Add comment · Show 2
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
avatar image Kennai · Aug 05, 2019 at 11:09 AM 0
Share

Hello, @$$anonymous$$istyisty123 ! I suggest you to check if your log prefab has a collider on a log object (not on his children). Because if you added collider to log children, then your hitInfo.collider is a children, not a root log object.
Also, do you see spawned log object in scene? and in hierarhy tree too? is it has position exactly where it must be?

avatar image Mistyisty123 Kennai · Aug 05, 2019 at 11:54 AM 0
Share

@$$anonymous$$ennai yes the prefab has a box collider on it, it has no children. When the log is spawned it is in the scene yes and is in the heirarhy tree as well. Position is all correct.

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

172 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

Related Questions

Make tagged objects explode with ray cast? 1 Answer

mouse position on tag 1 Answer

find GUIText 1 Answer

Raycast starts ignoring gameobject after a few seconds 0 Answers

Make an instantiated object match the slope of the terrain 1 Answer


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