Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 amna9028 · Jul 10, 2015 at 08:17 PM · c#unity 2d

UnityException: You are not allowed to call this function when declaring a variable.

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. UnityEngine.GameObject..ctor () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineGameObject.cs:448) OnTriggeringBullet..ctor () UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) paddle:Update() (at Assets/Scripts/paddle.cs:30)[ attachedBall =Instantiate(ballPrefab,ballPrefabRef.transform.position,Quaternion.identity) as GameObject;]

 ;;;;;;;;;;;;;;;;;;;;
 using UnityEngine;
 using System.Collections;
 
 public class paddle : MonoBehaviour {
    public float paddlespeed;
    public GameObject ballPrefab;
     public GameObject ballPrefabRef;
    GameObject attachedBall = null;
     // Use this for initialization
     void Start () {
  
     }
     
     // Update is called once per frame
     void Update () {
 
         float amtToMove = Input.GetAxis("Horizontal") * paddlespeed * Time.deltaTime;
         transform.Translate(Vector3.right * amtToMove);
 
         if (transform.position.x >= 4.40f)
             transform.position = new Vector2(4.40f, transform.position.y);
         if (transform.position.x <= -4.17f)
             transform.position = new Vector2(-4.17f, transform.position.y);
 
        
         
           if (Input.GetButtonDown("Jump") )
 
            {
                    attachedBall =Instantiate(ballPrefab,ballPrefabRef.transform.position,Quaternion.identity) as GameObject;
               
                    attachedBall.rigidbody2D.AddForce(Vector2.up*300);
                    Destroy(attachedBall,6);
              
            }
     }
   
 }

//////////////////////////////// /** The first if condition in OnTrigger Function doesn't work while second if condition does work Set Active doesn't work in both if condition...LeanTween is called but it doesn't work too.

 using UnityEngine;
 using System.Collections;
 
 public class OnTriggeringBullet : MonoBehaviour {
     public GameObject potassium_obj;
     public GameObject magnesium_obj;
     public GameObject iron_obj;
     public GameObject lead_obj;
    
 
     GameObject current_obj = new GameObject();
     int score ;
     int life ;
 
 
     public GameObject potassium1;
     public GameObject magnesium1;
 
 
     Vector2 potassium= new Vector2 (0.64f, 0.16f);
     Vector2 magnesium = new Vector2 (0.64f, 0.30f);
 
 
     // Use this for initialization
     void Start () {
         current_obj = potassium_obj;
         Debug.Log (current_obj);
         life = 2;
         score = 0;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter2D(Collider2D collision)
     {       //Magnesium balloon clicked
         if ((collision.gameObject.name == "balloon2") && (current_obj == magnesium_obj)) {
             score = score + 1;
             Debug.Log ("Score " + score);
             magnesium1.SetActive (true);
             LeanTween.move(magnesium1, magnesium, 10.0f);
             
             current_obj = iron_obj;
             Debug.Log ("Current Object " + current_obj);
             Destroy (collision.gameObject);
             //Play animation of balloon exploding up
             
             
         } 
         
         else {
             life = life - 1; 
             Debug.Log ("Else condition true");
             Debug.Log ("Lives left" + life);
             //popup appears notifying user of losing one life
         }
 
 
 
         //Potassium balloon clicked
                     if (collision.gameObject.name == "balloon" && current_obj == potassium_obj) {
                         score = score + 1;
                         Debug.Log ("Score " + score);
                         potassium1.SetActive (true);
                         LeanTween.move(potassium1, potassium, 1.0f);
                         
                         current_obj = magnesium_obj;
                         Debug.Log ("Current Object " + current_obj);
                         Destroy (collision.gameObject);
                         //Play animation of balloon exploding up
                         
                 } 
 
         else {
             life = life - 1; 
             Debug.Log ("Else condition true");
             Debug.Log ("Lives left" + life);
             //popup appears notifying user of losing one life
                 }
         /////////////////////////////////////////////////
 
 
         
     }
 }


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
1
Best Answer

Answer by barbe63 · Jul 10, 2015 at 09:04 PM

 GameObject current_obj = new GameObject(); 

This is the line causing the error. Just do:

 GameObject current_obj;
Comment
Add comment · Show 13 · 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 amna9028 · Jul 11, 2015 at 06:29 AM 0
Share

Thank you. It's working now. But OnTriggerFunction is not working the way it should work. Could you please help me address it? The second if condition does work thereby destroying object only but the two statements of SetActive and LeenTween are not working as well though leantween executes well. The first if condition doesn't work from the very beginning.

avatar image amna9028 · Jul 11, 2015 at 06:31 AM 0
Share

Since I have created prefab for my bullet gameobject. So I added a triggerEvent Script on bullet prefab. The gameObjs do not drag and drop on the inspector window. So I created their prefabs and dropped them on the script. The is trigger property of all gameobjects is checked.

avatar image amna9028 · Jul 11, 2015 at 06:36 AM 0
Share

Though OnCollisionEnter2D works well! But I want to use trigger function here!

avatar image barbe63 · Jul 11, 2015 at 09:38 AM 0
Share

It might be that none of the triggers have rigidbody attached to it so collision never occurs. Also check if your balloons have the right layers.

avatar image amna9028 · Jul 11, 2015 at 12:42 PM 0
Share

Checked all! there's a problem with if statements where variables are being compared. I started working on this script from scratch. I declared a variable for a check. First if condition works right. In second if condition, this cur_num variable does remain same that is 1. In first if condition, I'm changing variable value to 2 but in reality its not changed. and compares 1 with cur_num in second if condition. Now how to change variable value so that the second conditions works well. public int cur_num;

 void Start () {

     cur_num = 1;
 }
 
 


 void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.name == "balloon" && cur_num == 1) 
     {
         cur_num  = 2;
         Debug.Log (cur_num);

             }



     if (collision.gameObject.name == "mag_balloon" && cur_num == 2) 
     {
         Debug.Log ("collision with mag balloon");
             }
 }
Show more comments

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

,How to stop jump animtion 1 Answer

How to save different values with same script? 1 Answer

How to add a score when the correct object drags to the correct box? 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