NullReferenceException Fire Ball - c#-4.0

I'm working on this game breakout-game
And i'm trying to make the ball fire and collide with the wall bouncing, to do that I did what he said, I added a script and put this code:
public class BallMove : MonoBehaviour {
private Rigidbody rb;
public float ballVelocity = 800f;
private bool isMove;
// Use this for initialization
void awake() {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1") && isMove == false) {
transform.parent = null;
isMove = true;
rb.isKinematic = false;
rb.AddForce (new Vector3(ballVelocity,ballVelocity,0));
}
}
}
I understand every line of this code, but when I try to play this, I got a nullReferenceException ,I can run the game but when I press the key I'm getting a error,someone know why? and what happens?

You should use Awake(), not awake(). In your case, you are using a "customized" function, and not the "official" one used by the Unity engine.
So, the engine cannot start that function by itself, and rb stills null when used in the Update().
Example:
void Awake() {
rb = GetComponent<Rigidbody> ();
}

Related

Why does my RotateTransition throw errors after it runs for the first time?

Warning: This is my first time using threads and my first time trying out an animation. Please bear with me.
I want to rotate an ImageView. I set up a thread for it:
public class ThreadAnimation extends Thread
{
private ImageView iv;
private RotateTransition rt;
public ThreadAnimation(ImageView iv)
{
this.iv = iv;
}
#Override
public void run()
{
while (true)
{
RotateTransition r = new RotateTransition();
r.setToAngle(360);
r.setCycleCount(1);
r.setDuration(Duration.millis(300));
r.setNode(iv);
r.play();
try
{
sleep(100);
} catch (InterruptedException e)
{
return;
}
}
}
}
I call this inside my controller class, upon pressing a Button.
animation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle (ActionEvent abschicken)
{
ThreadAnimation thread = null; //ANIMATION PIZZA
if (thread == null)
{
thread = new ThreadAnimation(olivenview);
thread.start();
}
}
});
My ImageView olivenview will rotate just like I wanted it to. However it takes quite a long time until it seems to stop (I can see it because the button triggering it still looks triggered for a while) and when I go ahead to press it a second time afterwards, I get a nonstop error stream with a lot of null pointer exceptions. I am very clueless, can anyone help me out? Is this due to my Thread Setup or does the problem lie somewhere else (in code that I didn't post here)?
I believe you do not need threads for this. Notice the .play() method returns immediately and the animation will run in the background.
That being said, try this.
...
//Create your rotation
final RotateTransition r = new RotateTransition();
r.setToAngle(360);
r.setCycleCount(1);
r.setDuration(Duration.millis(300));
r.setNode(iv);
//When the button is pressed play the rotation. Try experimenting with .playFromStart() instead of .play()
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent action) {
r.play();
}
});
...
On an other note I recommend switching to java 8 so that you can use lambda expressions instead of the anonymous class!

check object destroyed and load new scene

I am extinguishing fire particles by smoke particles so then I want to check that object is destroyed or not, if object is destroyed then load new scene.
Here is my script,,,,,, Any suggestions ?
using UnityEngine;
using System.Collections;
public class hey : MonoBehaviour {
void Start(){
GetComponent<ParticleSystem> ().emissionRate = 0;
}
void Update(){
if (Input.GetMouseButtonDown (1)) {
GetComponent<ParticleSystem> ().Emit (20);
}
}
void OnParticleCollision(GameObject obj)
{
Destroy (obj, 2.0f);
//here i want to check and then load new scene..
//I try that thing, but failed..nothing happen
if (object.Equals (obj, null)) {
Application.LoadLevel (7);
}
//also this one, but nothing happens
if(gameObject.tag=="fire123"==null){
Application.LoadLevel (7);
}
// also this one too, but failed :-(
void OnDestroy(){
Application.LoadLevel (7); } }
The problem: Destroy(obj, **2.0f**);
Because of the delay the item is not destroyed when the if is evaluated.
So either don't use a delay or make OnParticleCollision a coroutine and use a yield return new waitForSeconds or put the check somewhere else like Update or use OnDestroy in a script on the particle that gets destroyed.

Unity3D : Retry Menu (Scene Management)

I'm making a simple 2D game for Android using the Unity3D game engine. I created all the levels and everything but I'm stuck at making the game over/retry menu. So far I've been using new scenes as a game over menu. I used this simple script:
#pragma strict
var level = Application.LoadLevel;
function OnCollisionEnter(Collision : Collision)
{
if(Collision.collider.tag == "Player")
{
Application.LoadLevel("GameOver");
}
}
And this as a 'menu':
#pragma strict
var myGUISkin : GUISkin;
var btnTexture : Texture;
function OnGUI() {
GUI.skin = myGUISkin;
if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
Application.LoadLevel("Easy1");
if (GUI.Button(Rect(Screen.width/2-90,Screen.height/2+100,170,40),"Main Menu"))
Application.LoadLevel("MainMenu");
}
The problem stands at the part where I have to create over 200 game over scenes, obstacles (the objects that kill the player) and recreate the same script over 200 times for each level. Is there any other way to make this faster and less painful?
Edit : If possible,please when you suggest your ideas,use javascript only,I don't understand C#,not even a little bit.I know Im asking too much but it realy confuses me.
Thank you.
There are several different solutions, but I would recommend using PlayerPrefs. This has the extra benefit of persisting even when the application is closed and then re-opened.
In your Awake() function of your Main Menu class, you can get the current level and store it in a static string of your Main Menu class. If it is the player's 1st time, use the name for level 1.
Something like this:
static string currentLevelName;
void Awake()
{
currentLevelName = PlayerPrefs.GetString("CurrentLevel");
if (currentLevelName == defaultValue)
{
currentLevelName = "Level1"
}
}
Then, modify your button to do this instead:
if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
Application.LoadLevel(currentLevelName);
Whenever the player advances to the next level, set the string in PlayerPrefs to the new level name:
PlayerPrefs.SetString("CurrentLevel", Application.loadedLevelName);
You can create a class with static properties. For example (in c#)
public class GameOverInput
{
public static string name;
public static string retryLevel;
//all the info you need
}
Then you can easily read the input in your game over scene (only one is needed)
public class GameOverMenu : MonoBehavior
{
void Start()
{
Debug.Log("You were killed by " + GameOverInput.name);
Application.LoadLevel(GameOverInput.retryLevel);
}
}
And you set this info just before loading the game over scene
if (Collision.collider.tag == "Player")
{
GameOverInput.name = "Baddie";
Application.LoadLevel("GameOver");
}
Another option would be to make something like a singleton LevelManager MonoBehavior and add it to an object named "Level Manager". Use the DontDestroyOnLoad function to make the object persist even when you load another level.
class LevelManager : MonoBehavior
{
static LevelManager _instance;
public string currentLevelName;
public string killedBy;
function Awake ()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject); // Make sure we never have more than 1 Level Manager.
}
}
LevelManager Instance
{
get
{
if (_instance == null)
{
GameObject levelManagerObject = GameObject.Find("Level Manager");
_instance = levelManagerObject.GetComponent<LevelManager>();
}
return _instance
}
}
}
Then, from the main menu class, you can always access the Level Manager like so:
Debug.Log("Killed by " + LevelManager.Instance.killedBy);
or
LevelManager.Instance.currentLevelName = Application.loadedLevelName;

MvvmCross binding iOS Gestures

I'm searching a way how can i bind ios gesture like UILongPressGestureRecognizer to ICommand or MvxCommand in MvvmCross, thanks.
PS : I found an example here but i can't figure out how to do that.
From the example you found and from the current MVVM Cross source I did the following
public static class MvxBehaviourExtensions
{
public static MvxLongPressGestureRecognizerBehaviour LongPress(this UIView view)
{
var toReturn = new MvxLongPressGestureRecognizerBehaviour(view);
return toReturn;
}
}
and
public class MvxLongPressGestureRecognizerBehaviour
: MvxGestureRecognizerBehavior<UILongPressGestureRecognizer>
{
protected override void HandleGesture(UILongPressGestureRecognizer gesture)
{
// Long press recognizer fires continuously. This will ensure we fire
// the command only once. Fire as soon as gesture is recognized as
// a long press.
if (gesture.State == UIGestureRecognizerState.Began)
{
FireCommand();
}
}
public MvxLongPressGestureRecognizerBehaviour(UIView target)
{
var lp = new UILongPressGestureRecognizer(HandleGesture);
AddGestureRecognizer(target, lp);
}
}
and to bind
set.Bind(this.LongPress()).For(lp => lp.Command).To(c => c.DoTheStuffCommand);

Blackberry - how to create a layout like sliding drawer android

Does anyone know how to create layout component in blackberry that behave like android's sliding drawer?
thx.
Haven't actually done it. But I think the biggest difficulty here is the Sliding animation part and the visibility IMHO. You should first work on the animation for the sliding effect. Then on the Manager itself.
Touch devices come with another difficulty... you have to program the slider touch event so that it follows the gesture.
hey its really a brilliant idea to have such layouts for blackberry. it is quite challenging to get it done. we have to play with custom layouts.
first thing requied is the knowledge of customization of managers. I believe we can also use pop up screen to slide it up. manager on top of an pop up screen.
secondly, the Gusture api for side scrolling.
all the best buddy.
Please check the below source code in which i created a manager class. Just add whatever you want to slide to that manager and use it. Change according to your requirement.
package mypackage;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
public class SlidingDrawer extends Manager {
final static int STATE_VIEW = 1;
final static int STATE_HIDE = STATE_VIEW+1;
final static int STATE_SLIDE_DOWN = STATE_HIDE+1;
final static int STATE_SLIDE_UP = STATE_SLIDE_DOWN+1;
final static int MAX_NO_OF_STATES = STATE_SLIDE_UP+1;
static int CURRENT_STATE = STATE_VIEW;
int i = 0;
LabelField _lbl_hero;
public SlidingDrawer(long arg0) {
super(arg0);
// TODO Auto-generated constructor stub
init();
}
private void init()
{
_lbl_hero = new LabelField("Hero testing Every thign....");
this.add(_lbl_hero);
}
protected void paint(Graphics graphics) {
// TODO Auto-generated method stub
switch(CURRENT_STATE)
{
case STATE_VIEW:
super.paint(graphics);
break;
case STATE_HIDE:
break;
case STATE_SLIDE_DOWN:
super.paint(graphics);
if(i<this.getHeight())
{
this.getField(0).setPadding(i++, 0, 0, 0);
invalidate();
}
else
{
CURRENT_STATE = STATE_HIDE;
}
break;
case STATE_SLIDE_UP:
super.paint(graphics);
if( i > 0 )
{
this.getField(0).setPadding(i--, 0, 0, 0);
invalidate();
}
else
{
CURRENT_STATE = STATE_VIEW;
}
break;
}
}
public void setState(int state)
{
if(state < MAX_NO_OF_STATES)
{
CURRENT_STATE = state;
}
else
Dialog.alert("Invalid State....");
}
protected boolean touchEvent(TouchEvent message) {
// TODO Auto-generated method stub
if(CURRENT_STATE == STATE_VIEW)
{
i=0;
CURRENT_STATE = STATE_SLIDE_DOWN;
invalidate();
}
else if(CURRENT_STATE == STATE_HIDE)
{
// i = this.getField(0).getContentRect().y;
CURRENT_STATE = STATE_SLIDE_UP;
invalidate();
}
return super.touchEvent(message);
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
setExtent(360, 100);//Width and Height of the Childs
}
}
Please change this code according to your requirement and use it.

Resources