• 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 Oct 17, 2015 at 03:15 AM by Bunny83 for the following reason:

Question is off-topic or not relevant

avatar image
1
Question by ZachAttack9280 · Oct 16, 2015 at 07:24 PM · tree

Tree Chopping

So in my game, i programed so the first person (me) whould chop down a tree, then it would fall and coconuts and wood would spawn next to it. but i have 3 errors that i could not find how to fix. ( i even did some research).

Here are the errors:

Assets/Part 4 - Tree Chopping/TreeController.js(14,49): BCE0043: Unexpected token: ).

Assets/Part 4 - Tree Chopping/TreeController.js(14,50): BCE0044: expecting ), found '.'.

Assets/Part 4 - Tree Chopping/TreeController.js(14,51): UCE0001: ';' expected. Insert a semicolon at the end.

Here is the code:

code:

 #pragma strict
 
 var treeHealth : int = 5;
 
 var logs : Transform;
 var coconut : Transform;
 var tree : GameObject;
 
 var speed : int = 8;
 
 function Start()
 {
     tree = this.gameObject;
     this.gameObject.GetComponent<Rigidbody>().isKinematic = true;
 
 }
 
 function Update()
 {
     if(treeHealth <= 0)
     {
         rigidbody.isKinematic = false;
         rigidbody.AddForce(transform.forward * speed);
         DestroyTree();
     }
 }
 
 function DestroyTree()
 {
     yield WaitForSeconds(7);
     Destroy(tree);
     
     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
     Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
 }
 

Is there something i did wrong?

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Badgerfish · Oct 17, 2015 at 02:46 AM

Your Destroy(tree) function is running before you spawn the objects that depend on the tree's location.

Tip: Spawn(instantiate) the objects first, then Destroy(tree).

Why? It happens in the same frame so the order doesn't make a difference to the human eye.

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
avatar image
1

Answer by NewPath · Oct 16, 2015 at 08:00 PM

Your main problem is cargo-cult programming. There are actually quite a few errors in your code, but I'll answer your question:

You've copy-pasted code from the wrong language. JS doesn't use the same generics syntax as C#. Unity has provided some helper methods though, so you can fix the immediate error quite easily by inserting a dot in front of your opening angle brace:

 gameObject.GetComponent.<Rigidbody>().isKinematic = true;



 
Comment
Add comment · Show 5 · 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 ZachAttack9280 · Oct 16, 2015 at 09:49 PM 0
Share

but now i get more errors,

Assets/Part 4 - Tree Chopping/TreeController.js(23,17): BCE0144: 'UnityEngine.Component.rigidbody' is obsolete. Property rigidbody has been deprecated. Use GetComponent() ins$$anonymous$$d. (UnityUpgradable)

Assets/Part 4 - Tree Chopping/TreeController.js(23,27): BCE0019: 'AddForce' is not a member of 'UnityEngine.Component'.

Here is my code again:

 #pragma strict
 
 var treeHealth : int = 5;
 
 var logs : Transform;
 var coconut : Transform;
 var tree : GameObject;
 
 var speed : int = 8;
 
 function Start()
 {
     tree = this.gameObject;
     gameObject.GetComponent.<Rigidbody>().is$$anonymous$$inematic = true;
 
 }
 
 function Update()
 {
     if(treeHealth <= 0);
     {
         gameObject.GetComponent.<Rigidbody>().is$$anonymous$$inematic = false;
         rigidbody.AddForce(transform.forward * speed);
         DestroyTree();
     }
 }
 
 function DestroyTree()
 {
     yield WaitForSeconds(7);
     Destroy(tree);
     
     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
     Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
 }
 
avatar image NewPath ZachAttack9280 · Oct 16, 2015 at 10:02 PM 0
Share

Yes, that's why I said you had quite a few errors in your code.

$$anonymous$$y original statement about cargo-cult program$$anonymous$$g stands, your biggest problem is that you are just copy-pasting code from various sources and you have no idea what it means or does. I'm personally not going to sit here and rewrite it for you, but maybe someone else will.

avatar image ZachAttack9280 NewPath · Oct 16, 2015 at 11:13 PM 0
Share

and first of all, helping is a good thing to do, to me, that was pretty rude, i may understand lots of stuff but maybe not this so i would suggest you to help, or go tell everyone that same thing, were all here for a reason.

avatar image ZachAttack9280 · Oct 16, 2015 at 10:14 PM 0
Share

Never$$anonymous$$d, i fixed that but in a new code i get this error:

Assets/Part 4 - Tree Chopping/RayCastTree.js(7,26): BCE0018: The name 'PlayerController' does not denote a valid type ('not found'). Did you mean 'UnityEngine.Networking.PlayerController'?

Here is the code:

code:

 #pragma strict
 
 var rayLength = 10;
 
 private var treeScript : TreeController;
 
 private var playerAnim : PlayerController;
 
 private var canSwing : boolean = true;
 
 function Update()
 {
     var hit : RaycastHit;
     var fwd = transform.TransformDirection(Vector3.forward);
     
     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
     {
         if(hit.collider.gameObject.tag == "Tree")
         {
             treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent(TreeController);
             playerAnim = GameObject.Find("FPSArms_Axe@Idle").GetComponent(PlayerController);
             
             if(Input.GetButtonDown("Fire1") && playerAnim.canSwing == true)
             {
                 treeScript.treeHealth -= 1;
             }
         }
     }
 }

I seem to get this error a lot, can you please explain what the error means and what i did wrong cause i haven't found anything on the internet. Thanks.

avatar image Bunny83 · Oct 17, 2015 at 03:13 AM 0
Share

@ZackAttack9280
He wasn't rude, he was actually quite nice since he answered your question. According to our $$anonymous$$oderation Guidelines Your question(s) fall into the category "Debugging questions" since you just continue to post the next error and expect us to debug your code.

Here's a link to the learning page.

Just like NewPath mentioned you seem to have problems with the basic syntax rules of your choosen language, so i suggest you look through some tutorials.

Also have a look at the FAQs of this site especially the section "Why did my question get closed or rejected?"

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

33 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

Related Questions

Tree Creator Shader will not fade 0 Answers

How to Cut a Mesh - The Forest Style Tree Cutting 0 Answers

Vertex Animation Shader Performance (tree vegetation) 3 Answers

Tree Painting Breaks Prefab/Model Appearance 1 Answer

How does one adjust tree bend factor in animation timeline? 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