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 axcher · Jul 11, 2014 at 03:45 PM · variabledeclaration

UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration

Hi everybody! I'm new to game development and programming and I've got a little problem.

 #pragma strict
 
 var otherObj = gameObject; // The sword
 var Damage : int = 50;
 
 function OnCollisionEnter (hit : Collision)
 {
     if (otherObj.animation["Attack"].enabled)
     {
         if(hit.gameObject.tag == "enemy")
         {
             hit.gameObject.SendMessage("ApplyDamage", Damage);
         }    
     }
 }

The code seems to be working but I always get an error (UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration.).

What is wrong and what should I do?

I tried reading answers on similar questions but I could barely understand them. Please, explain it in simple words so that a newbie like me will understand. :)

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

1 Reply

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

Answer by meat5000 · Jul 11, 2014 at 03:48 PM

 var otherObj = gameObject; // ?
 

try

 var otherObj : GameObject; // :)

You are trying to dynamically create a variable of type GameObject and make it equal to the gameObject the script is attached to.

#pragma strict won't let you do that. Cuz its strict mode.

Also, When you use a capital letter for something like GameObject or Transform, it refers to the Type. So, 'of this Type'.

edit : See Bunny83's corrections below. All thumbs up go there ;)

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 axcher · Jul 11, 2014 at 04:04 PM 0
Share

The error's gone and a new lesson is learnt, thank you very much!)

avatar image Bunny83 · Jul 11, 2014 at 04:14 PM 2
Share

That variable isn't dynamic. It's statically typed and is allowed even in strict mode. The problem is the "gameObject" property which you can't use in a field initializer since those are executed from the loading thread.

Your solution is correct but to initialize the variable you usually would do this:

 var otherObj : GameObject
 
 function Awake()
 {
     otherObj = gameObject;
 }

However in most cases you would initialize public variables in the inspector by dragging the desired object onto the variable. I guess that the OP actually doesn't want to reference the own GameObject.

ps: a dynamic variable declaration would look like this:

 var someVar;

When you use a field initializer (so when you assign something to the variable when you declare it) the variable is statically typed. The type is inferred from the expression you assigned.

example:

 var someVar = 4;     // statically typed as int
 var someVar2 = 2.0;  // statically typed as float
 var someVar3;        // dynamic variable, doesn't work when using pragma strict.
 function Start()
 {
     someVar = 5.0;            // error, can't assign float to int
     someVar2 = "Hello world"; // error, can't assign string to float
     someVar3 = 5.0;           // should work
     someVar3 = "Hello World"; // should work
 }

avatar image meat5000 ♦ · Jul 11, 2014 at 04:24 PM 0
Share

Thanks @Bunny83 :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Declaring List variable for subclasses with baseclass or without type? 1 Answer

Scripting error! 1 Answer

Range of Unitys Variables 2 Answers

Camera issue (storing temp variable C# problem) 3 Answers

Random time interval 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