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 /
avatar image
0
Question by pocikanec · Nov 23, 2010 at 03:20 PM · errorsyntax-erroruce0001

GetComponent Script and Some basic JavaScript help

I got 2 scripts.

spawnCrate.js

var cratePrefab : Transform; var cratePocet = 0;

function Update () {

if (Input.GetButtonDown("Fire2") && cratePocet >= 1) { var crate = Instantiate(cratePrefab, GameObject.Find("cratespawn").transform.position, Quaternion.identity); cratePocet--; print("Crate spawned!"); }

}

and addCrate.js

var setObject : GameObject.Find("Character (Lerpz)"); var script : SpawnCrate;

function OnTriggerEnter ( other : Collider ) {

setObject.script = Component.GetComponent("SpawnCrate"); setObject.script.cratePocet++;

}

This is my idea: On start, my character's cratePocet will be set to 0. If he triggers the trigger on object called addCratespin, it will + 1 to cratePocet of SpawnCrate.js

SpawnCrate.js is attached on character, addCrate.js is on addCratespin object. Iam getting this error:

Assets/Scripts/Objects/addCrate.js(2,32): UCE0001: ';' expected. Insert a semicolon at the end.

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

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

Answer by skovacs1 · Nov 23, 2010 at 03:40 PM

Problems:

  1. GameObject.Find("Character (Lerpz)"); is not a type. Perhaps you meant var setObject : GameObject = GameObject.Find("Character (Lerpz)"); ?
  2. You shouldn't(can't?) call GameObject.Find in an object initialization. You would need to put it in Start.
  3. You shouldn't use GameObject.Find at all if you can avoid it. It is terribly expensive. In this case, you might consider simply setting the reference in the editor. If you must use a Find, try GameObject.FindWithTag in stead.
  4. setObject has a variable called .script? GameObjects don't have any such variable. What type is it supposed to be?
  5. Is there an instance of SpawnCrate attached to this GameObject? If not, then why are you treating it like it is?

A corrected script would look like:

var setObject : GameObject; var script : SpawnCrate;

function Start() { //setObject = GameObject.FindWithTag("Player"); //If you must... //setObject = GameObject.Find("Character (Lerpz)"); //as a last resort script = setObject.GetComponent("SpawnCrate"); }

function OnTriggerEnter ( other : Collider ) { if(script) script.cratePocet++; }

As a point though, an alternative approach is to not store the references in this script at all but to figure it out in OnTriggerEnter. Something like:

function OnTriggerEnter ( other : Collider ) {
    if(other.tag == "Player") {
    //if(other.name == "Character (Lerpz)") { //Valid alternative
        var script : SpawnCrate = other.GetComponent("SpawnCrate");
        if(script) script.cratePocet++;
    }
}
Comment
Add comment · Show 2 · 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 pocikanec · Nov 23, 2010 at 04:47 PM 0
Share

thanks! it works! :) can you explain me the first version of corrected script? why it was keeping saying to me, that i dont have attached on character (lerpz) [player] SpawnCrate, but i have? Or in something else was problem?

avatar image skovacs1 · Nov 23, 2010 at 07:52 PM 0
Share

Problem 4 was the biggest one as you were pretending that GameObject had a variable called 'script' which it does not. Problem 5 had nothing to do with SpawnCrate being attached to Lerpz because it didn't check Lerpz. When you called Component.GetComponent (or you could have just called GetComponent), you were getting the component from the current GameObject this script was attached to (AddCrateSpin).

avatar image
0

Answer by pocikanec · Nov 23, 2010 at 03:51 PM

Now, it shows me this errors:

Assets/Scripts/Objects/addCrate.js(10,15): BCE0019: 'script' is not a member of 'UnityEngine.GameObject'.

Assets/Scripts/Objects/addCrate.js(10,34): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.

Assets/Scripts/Objects/addCrate.js(11,15): BCE0019: 'script' is not a member of 'UnityEngine.GameObject'.

Assets/Scripts/Objects/addCrate.js(11,22): BCE0049: Expression cannot be assigned to.

As said up, i have attached addCrate to addCratespin - a cube, wich rotates and SpawnCrate to Character (Lerpz) - that are names in hierarchy.

I have deleted the //If you must (only //) and / + /

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 skovacs1 · Nov 23, 2010 at 04:19 PM 0
Share

Is this an answer to your question? If not, then do not post it as an answer. Add information like this to the original question or in comments on the answer you are referring to.

avatar image skovacs1 · Nov 23, 2010 at 04:27 PM 0
Share

The commenting was just for completeness, hence the comment. Feel free to uncomment as you like - that was the point. $$anonymous$$nowing what is attached to what has no bearing on your problem here other than indicating that you are trying to get a component that doesn't exist on the GameObject you are trying to get it from. I will revise my answer with these further errors of yours that I didn't even notice.

avatar image ben · Mar 15, 2011 at 10:43 AM 0
Share

place the script u wnna access first in standard assets folder see this file:///Applications/Unity%203.1/Unity.app/Contents/Documentation/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

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

No one has followed this question yet.

Related Questions

Collision Detector script 1 Answer

Limit rotation for a statue puzzle 1 Answer

script help 2 Answers

Semicolon? WHAT? HELP ME! 1 Answer

Desintergrate Enemies on Dying 4 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