- Home /
TornadoTwins Tutorial Problem
Hi
I have been following the TornadoTwins Tutorial and I have done exactly what they do and I get errors everytime.
Here is my script to make my character move and shoot fireballs:
var BulletPrefab = UnityEngine.Transform;
var speed = 3.0;
var rotateSpeed = 3.0;
function Update () { var controller : CharacterController=GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
if(Input.GetButtonDown("Jump"))
{
var Bullet = Instantiate (BulletPrefab, UnityEngine.GameObject.Find("Spawn"),
UnityEngine.Transform.position.identity, UnityEngine.Vector3, UnityEngine.Quaternion.identity);
}
}
Here is the error I get
Assets/Scripts/Worm.js(15,47): BCE0020: An instance of type 'UnityEngine.Transform' is required to access non static member 'position'.
What should I do?
Answer by Jesus_Freak · Nov 08, 2010 at 12:47 AM
well, i also have a modified tornado twins movearound script, but it looks like you are trying to make it a C+ script with the "UnityEngine."
you don't have to do that since this is (or should be) a javascript.
sooo... here's an example of what that should look like:
var Bullet = Instantiate(BulletPrefab, gameObject.Find("Spawn").transform.position,
Quaternion.identity);
yeah, it looks like you were making a script hybrid, half java, half C+
so just use this example to fit your needs.
You need a capital "G" in "GameObject.Find();" otherwise you will get the opposite error he did :). And, what is C+? Are you referring to C++?
oh sorry, in unity, it says C Sharp script, but the symbol it uses in the project tab, it looks like a "+" but it's actually a sharp "#" sign. but anyway, you still don't have to do the "UnityEngine." and also do what Peter G said. that'd help.
Answer by Peter G · Nov 08, 2010 at 01:11 AM
To further explain the error, position is an instance variable (it's a property, but Unity does not distinguish the difference in js) so you must have an instance of a Transform to access that variable.
You treated it like a class variable, meaning you can access without an instance of the object, hence, it outputted the "non-static member".
To solve the problem, you simply need to find an instance in the scene. Creating a new Transform programmatically unattached to a GameObject would be a bad idea. So, using various methods, you must find a transform, then you can say transform.position.
For example:
function Update () {
if(Input.GetButtonDown("Jump"))
{
var Bullet : GameObject = Instantiate (BulletPrefab, GameObject.Find("Spawn").transform.position, UnityEngine.Quaternion.identity);
}
}
In javascript, Unity also includes using UnityEngine;
automatically so you do not have to reference the namespace when you access the class.
I am unsure how familiar you are with class vs. instance methods and functions so I might recommend reading some tutorials on programming concepts if you are new.
Thanks a bunch but can i just say that I get less errors if i use the UnityEngone. I no it isnt normal but ti just dosnt come up with errors so i thought i might as well put it in.
Anyway thanks for answering my Q.
Answer by Unknown5639 · Nov 09, 2010 at 06:11 PM
Right I have a slight problem: I tried both of the above and I have tried and tried her is what my script looks like now.
var speed = 3.0; var rotateSpeed = 3.0;
function Update (){
var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
if(Input.GetButtonDown("Jump"))
{
Instantiate (GameObject.Find("FireBall"),
GameObject.Find("Spawn").transform.position,
Quaternion.identity);
}
}
Here are the errors I get
UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. Worm..ctor () (at Assets/Scripts/Worm.js:3)
NullReferenceException UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at E:/BuildAgent/work/68355d6e5d19d587/Runtime/Export/Generated/BaseClass.cs:46) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at E:/BuildAgent/work/68355d6e5d19d587/Runtime/Export/Generated/BaseClass.cs:57) Worm.Update () (at Assets/Scripts/Worm.js:14)
You will get better responses if you format you code properly. It makes it much easier to read, hence less time wasted deciphering your writing, and more time solving your problem :)
I am not getting any errors when I run your script. The only one I can see happening is the NullReference. $$anonymous$$ake sure that you have a GameObject named "FireBall" and "Spawn" in your scene.
Yes I do but I now have a working script thanks for all your help.!!
Answer by meran-23 · Mar 03, 2011 at 05:16 PM
Here is the script. FireBall needs to be called:FireBall Your spawn point needs to be called:Spawn
var speed = 3.0; var rotateSpeed = 3.0; var FireBall: Transform;
function Update () { var controller : CharacterController=GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward curSpeed); transform.Rotate(0, Input.GetAxis ("Horizontal") rotateSpeed, 0);
if(Input.GetButtonDown("Jump")) { var Bullet = Instantiate(FireBall, gameObject.Find("Spawn").transform.position, Quaternion.identity);
}
} @script RequireComponent(CharacterController)
Your answer
![](https://koobas.hobune.stream/wayback/20220613082240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wrong with script 1 Answer
An instance of type 'UnityEngine.Component' is required to access non static member 1 Answer
An instance of type "x" is required to access non-static variable "y" 2 Answers
An instance of type 'TurretControl' is required to access non static member 'Shoot'. 1 Answer
tornado twins worm game tutorial turret not firing 2 Answers