- Home /
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.
Answer by skovacs1 · Nov 23, 2010 at 03:40 PM
Problems:
GameObject.Find("Character (Lerpz)");
is not a type. Perhaps you meantvar setObject : GameObject = GameObject.Find("Character (Lerpz)");
?- You shouldn't(can't?) call GameObject.Find in an object initialization. You would need to put it in Start.
- 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.
- setObject has a variable called .script? GameObjects don't have any such variable. What type is it supposed to be?
- 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++;
}
}
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?
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).
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 / + /
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.
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.
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
Follow this Question
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