• 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 Unknown5639 · Nov 07, 2010 at 05:16 PM · errortornado-twinsbce0020

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?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

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 Peter G · Nov 08, 2010 at 01:12 AM 0
Share

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++?

avatar image Jesus_Freak · Nov 09, 2010 at 02:02 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Show 1 · 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 Unknown5639 · Nov 09, 2010 at 04:00 PM 0
Share

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.

avatar image
0

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)

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 Peter G · Nov 09, 2010 at 10:39 PM 0
Share

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 :)

avatar image Peter G · Nov 09, 2010 at 10:48 PM 0
Share

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.

avatar image Unknown5639 · Nov 10, 2010 at 07:23 PM 0
Share

Yes I do but I now have a working script thanks for all your help.!!

avatar image
0

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)

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

what's wrong with this?? 2 Answers

An instance of type 'UnityEngine.Component' is required to access non static member 1 Answer

Scripting Errors from tutorial I was following 2 Answers

Simple AddForce Scirpt help 1 Answer

An instance of type "x" is required to access non-static variable "y" 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges