Hi guys. My experience is very small. And my english bad.
And Google in 10 hours could not help me.
I have 3 script
- cameraController
- bg
- bubbleMove
cameraController contains a system of points.
bg creates object clones from prefab, prefab have script bubbleMove, and when the condition inside the script right, points in cameraController must be plus, but unity3d say me:
“NullReferenceException: Object reference not set to an instance of an object
bubbleMove.Update () (at Assets/bubbleMove.cs:18)”.
What am I doing wrong? How to deal?
I’m attach the screens and scripts.
cameraController
using UnityEngine;
using System.Collections;
public class cameraController : MonoBehaviour {
public bubbleMove bubbleMove;
public Vector3 mousPos;
public Vector3 mousePos;
public Vector3 worldPos;
public int width;
public int height;
public Vector2 screenSize;
public Vector2 worldScreenSize;
public int score = 0;
public int scorePlus = 1;
// Use this for initialization
void Start () {
screenSize = new Vector2(width, height);
worldScreenSize = Camera.main.ScreenToWorldPoint(screenSize);
score = 0;
scorePlus = 1;
width = Screen.width;
height = Screen.height;
}
// Update is called once per frame
void Update () {
print("scoreFrom cameraController="+score);
}
}
bg
using UnityEngine;
using System.Collections;
public class bg : MonoBehaviour {
public cameraController cameraController;
public GameObject RandB;
public int bubbleCount;
float d;
private int i;
private Vector2 mapSize;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
mapSize = new Vector2(cameraController.worldScreenSize.x, cameraController.worldScreenSize.y);
for (int i = 0; d + 1 < Time.time && i < bubbleCount; i++ )
{
GameObject bubbleClone1;
bubbleClone1 = Instantiate(RandB, new Vector2(Random.Range(-mapSize.x, mapSize.x),
Random.Range(-mapSize.y, mapSize.y)), RandB.transform.rotation) as GameObject;
GameObject bubbleClone2;
bubbleClone2 = Instantiate(RandB, new Vector2(Random.Range(-mapSize.x, mapSize.x),
Random.Range(-mapSize.y, mapSize.y)), RandB.transform.rotation) as GameObject;
GameObject bubbleClone3;
bubbleClone3 = Instantiate(RandB, new Vector2(Random.Range(-mapSize.x, mapSize.x),
Random.Range(-mapSize.y, mapSize.y)), RandB.transform.rotation) as GameObject;
GameObject bubbleClone4;
bubbleClone4 = Instantiate(RandB, new Vector2(Random.Range(-mapSize.x, mapSize.x),
Random.Range(-mapSize.y, mapSize.y)), RandB.transform.rotation) as GameObject;
d = Time.time;
}
}
}
bubbleMove
using UnityEngine;
using System.Collections;
public class bubbleMove : MonoBehaviour {
public cameraController cameraController;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (-Vector3.up * 0.1001f);
if(transform.position.y < 0)
{
cameraController.score = cameraController.score + cameraController.scorePlus;
print("score from bubbleMove="+cameraController.score);
Destroy (gameObject);
}
}
}