- Home /
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
}
/////////////////////////////////////////////////
}
}
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;
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.
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.
Though OnCollisionEnter2D works well! But I want to use trigger function here!
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.
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");
}
}
Your answer
Follow this Question
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