- Home /
send SCORE from OBJECT(clone) to other file
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);
}
}
}
Answer by mattyman174 · Sep 28, 2014 at 05:45 AM
public cameraController cameraController;
In your bubbleMove script, your cameraController is never given an initial value.
You should initialize it within your Start() method.
Hi guys, i can't did it right. I need that: If CLONE have y<0, cameraController from $$anonymous$$ain Camera doing score+1. cameraController from $$anonymous$$ain Camera show in console all score.
Please show me, how i can do it...
Answer by xuanyusong · Sep 28, 2014 at 06:34 AM
public cameraController cameraController; cameraController must be rename
public class bubbleMove : MonoBehaviour { public cameraController cameraController; // Use this for initialization void Start () { cameraController = gameObject.AddComponent(); }
// 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);
}
}
}
Hi guys, i can't did it right. I need that: If CLONE have y<0, cameraController from $$anonymous$$ain Camera doing score+1. cameraController from $$anonymous$$ain Camera show in console all score.
Please show me, how i can do it...
Answer by Maestro Beginner · Sep 29, 2014 at 12:59 AM
SOLVE
nowScorePlus = GameObject.Find ("Main Camera").GetComponent<cameraController> ().scorePlus;
nowScore = GameObject.Find ("Main Camera").GetComponent<cameraController> ().score;
nowScore = nowScore + nowScorePlus;
GameObject.Find("Main Camera").GetComponent<cameraController>().score = nowScore;
add this Strings in bubbleMove file. I'm so cool!!)) this working!!! 4 strings, and i'm solve this 15hours
Your answer
![](https://koobas.hobune.stream/wayback/20220613163922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I assign a script to a clone through another script? 1 Answer
How to stop all StopAllCoroutines(); from another script 1 Answer
How to make camera follow to clone player not first production ? 0 Answers
OnControllerColliderHit counting twice towards score 0 Answers
Scoresystem wont function correctly 0 Answers