Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Ethron981 · Aug 05, 2015 at 09:05 PM · c#unity 5gameobjectraycastprefab

Spawned tree not falling

**I have two scripts, one to give the full tree health (Tree Health), which when it reaches 0 spawns a chopped tree. This works correctly and as expected, but when the chopped tree is spawned, the top half of the tree does not fall over D:! I have applied a capsule collider, a rigid body and the script (Push Tree) to the part of the tree that should fall (the stump stays) but it doesn't seem to fall. I have also applied a capsule collider to the stump of the cut tree. Anyway, here are the two scripts (C#):

Tree Health:


 using UnityEngine;
 
 using System.Collections;
 
 public class TreeHealth : MonoBehaviour {

 public int Health;
 public GameObject FallenTree;
 public Camera myCamera;
 void Start () {
     myCamera = GameObject.FindObjectOfType<Camera>();
 }
 void Update ()
 {
     if(Health > 0)
     {
         if(Vector3.Distance(transform.position, myCamera.transform.root.transform.position) < 15f)
         {
             if(Input.GetKeyDown(KeyCode.Mouse0))
             {
                 Ray ray = new Ray(myCamera.transform.position,myCamera.transform.forward);
                 RaycastHit hit;
                 if(Physics.Raycast(ray,out hit,15f))
                 {
                     if(hit.collider.gameObject == gameObject)
                     {
                         --Health;
                     }
                 }
             }
         }
     }

     if(Health <= 0)
     {
         Health = 0;
         Destroy(gameObject);
         Instantiate(FallenTree,transform.position,transform.rotation);
     }
 }

}

And here is the Push Tree script I am presumably having problems with:

 using UnityEngine;
 
 using System.Collections;
 
 public class PushTree : MonoBehaviour {
 public float Force;
 public Camera MyCamera;

 void Start ()
 {
     MyCamera = GameObject.FindObjectOfType<Camera>();
     GetComponent<Rigidbody>().AddTorque(MyCamera.transform.right * Force,ForceMode.Impulse);
 }
 
 // Update is called once per frame
 void Update () {
 
 }

}

If no one can find the problem in this code then any suggestions about the prefabs would also be gladly appreciated! :)

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Aug 05, 2015 at 09:56 PM

Try moving your PushTree Start() code to Awake(). Instantiated objects and Start() sometimes have issues, sorry I can't explain the details but try it.

Comment
Add comment · Show 3 · 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
avatar image Ethron981 · Aug 06, 2015 at 01:36 PM 0
Share

Do you mean replace the void start() with void awake()? If I have got it right then it didn't change anything...

avatar image getyour411 · Aug 07, 2015 at 11:02 PM 0
Share

Actually I just noticed you are applying your physics forces on $$anonymous$$yCamera, I assume you meant to do that on tree?

avatar image Ethron981 · Aug 08, 2015 at 10:00 AM 0
Share

I have actually re-done my script now and it is quite different from this one, and the tree chopping works fine. But I do see that that was probably the problem anyway, I don't know how I missed it! :P. Anyway, I have a working script but thanks for your time nonetheless.

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

2 People are following this question.

avatar image avatar image

Related Questions

Game Object is Instantiating on same position 2 Answers

How to destroy a particular clone on touch/mouse? 1 Answer

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

Change Transparency 1 Answer

Unity - Remove GameObject 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