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
}
/////////////////////////////////////////////////
}
}