I have implemented a simple 2d game in unityscript. I have a prefab with RigidBody2D,Circle Collider2D and a script attached to it[else doesnt perform simple lerp also]. In the scene I have created an empty game object and attached the same script and rigidbody2d.
The script instantiates the prefab itself, which I attach in the inspector. Worked perfectly.
I am now translating the whole project into C#. But when I follow the similar steps as above, I get an error of UnassignedReferenceException and NullException. I know that it is caused because, I am not attaching anything to the script that I have attached to the prefab. But attaching anything to the prefab script causes more errors and seems wrong.
Is there some fundamental rule that I have missed?
using UnityEngine;
using System.Collections;
public class blobScript : MonoBehaviour {
private sceneManagerScript scenemanager;
public GameObject blobPrefab; //Add from inspector.
private GameObject blobobj; //Instantiate the blob prefab to this object.
public GameObject tutArrow; //Tutorial arrow prefab.
private GameObject[] tutobjs = new GameObject[3];
private float[] tutPositions = new float[3]{3.1f,2.7f,2.3f};
/*****Vectors*****/
private Vector2 tempBlobScale; //Tempvariable since blobobj's transform cannot be modified directly in C#.
private Vector2 mouseDownPos;
private Vector2 mouseUpPos;
private Vector2 newPosition;
private Vector2 v2delta;
private Vector2 tempNewPosition;
/*****INTEGERS*****/
private static int SCORE;
private static int LIVES;
/*****Flags*****/
private bool blobSet;
private bool blobScaledUp;
private bool CORRECT;
private bool isCorrectWallSet;
private bool diamondAnimation;
private bool tutorialset; //Start the tutorial.
private bool tutAnimating; //Start the animation.
private bool[] animTutArrows= new bool[3]; //Used to animate the tutorial.
private Color tempColor;
//CORRECT MOVE OR NO
public enum SWIPE {
RIGHT,
TOP,
LEFT,
BOTTOM
}
public SWIPE Swipe;
void Start(){
scenemanager=GameObject.Find("SceneManager").GetComponent<sceneManagerScript>();
tempBlobScale=Vector2.zero;
blobSet=false;
blobScaledUp=false;
CORRECT=false;
tutorialset=false;
SCORE=0;
LIVES = PlayerPrefs.GetInt("LIVES");
}
void Update(){
if(scenemanager.isState(sceneManagerScript.GAMESTATE.INITIALIZE)){
if(!blobSet){initBlob();} //<<--UNASSIGNED REFERENCE ERROR
if(blobSet){scaleUpBlob();}
}
if(scenemanager.isState(sceneManagerScript.GAMESTATE.PLAY)){
if(SCORE==0){
if(!tutorialset){
setTutorial();
}else{setTutorialAnim();} //Start animating the tutorial.
}
newPosition=Vector2.zero;
if (Input.GetMouseButtonDown (0)) {
Debug.Log("MOUSE DOWN!!");
mouseDownPos = Input.mousePosition;} //Save position when Screen Touched.
if (Input.GetMouseButtonUp (0)) {
Debug.Log("MOUSE UP!!");
mouseUpPos = Input.mousePosition; // api call to give the last position// user touched/mouse down
v2delta = mouseUpPos - mouseDownPos; // difference between them
if (v2delta.normalized.x >= CONSTANTS._TOUCHTHRESHOLD) {
newPosition.x = CONSTANTS._DISTANCE;
changeSwipeTo(SWIPE.RIGHT);
Debug.Log("Swipe is : "+Swipe);
} else if (v2delta.normalized.x <= CONSTANTS._TOUCHTHRESHOLD) {
newPosition.x = -(CONSTANTS._DISTANCE);
changeSwipeTo(SWIPE.LEFT);
Debug.Log("Swipe is : "+Swipe);
} else if (v2delta.normalized.y <= CONSTANTS._TOUCHTHRESHOLD) {
newPosition.y = -(CONSTANTS._DISTANCE);
changeSwipeTo(SWIPE.BOTTOM);
Debug.Log("Swipe is : "+Swipe);
} else if (v2delta.normalized.y >= CONSTANTS._TOUCHTHRESHOLD) {
newPosition.y = CONSTANTS._DISTANCE;
changeSwipeTo(SWIPE.TOP);
Debug.Log("Swipe is : "+Swipe);
}
}
if(newPosition!=Vector2.zero){
if(Swipe.ToString().Equals(scenemanager.getCorrectWall().ToString())){
tempNewPosition=newPosition;
scenemanager.changeStateToSwiped();
mouseUpPos = Vector3.zero;
v2delta = Vector2.zero;
}else if(LIVES > 0){
scenemanager.changeStateToWrongmove();
}
else{
tempNewPosition=newPosition;
scenemanager.changeStateToWrongmove();
mouseUpPos = Vector3.zero;
v2delta = Vector2.zero;
}
}
}
}
void FixedUpdate(){
if(scenemanager.isState(sceneManagerScript.GAMESTATE.SWIPED)){
blobobj.transform.position = Vector2.Lerp (blobobj.transform.position, tempNewPosition, Time.deltaTime*7); //<<--NULL REFERENCE ERROR
}
}
/*This function instantiates the blob after the play button is pressed*/
private void initBlob(){
int randomIndex = Random.Range(0,4); //To pickup a random color from the currentArrayOfColor[] in scenemanager.
blobobj=(GameObject)Instantiate(blobPrefab,Vector2.zero,Quaternion.identity);
blobobj.name=CONSTANTS._BLOBNAME;
blobobj.renderer.material.color =scenemanager.currentArrayOfColor[randomIndex]; //Set random color.
blobobj.renderer.material.shader=CONSTANTS._SHADER;
blobobj.transform.localScale= Vector2.zero; //Set scale to 0 since we have to scale up the blob.
blobSet=true;
}
/*This function scales up the blob after it has been instantiated*/
private void scaleUpBlob(){
if(tempBlobScale.x<1.0f){tempBlobScale=blobobj.transform.localScale;}
if(tempBlobScale.y<1.0f){
tempBlobScale.x+=CONSTANTS._SCALEUPFACTOR;
tempBlobScale.y+=CONSTANTS._SCALEUPFACTOR;
blobobj.transform.localScale=tempBlobScale;
}else{
tempBlobScale.x=1.0f;
tempBlobScale.y=1.0f;
blobobj.transform.localScale=tempBlobScale;
blobScaledUp=true;
}
}
public bool isBlobScaledUp(){return blobScaledUp;}
public bool getCORRECT(){return CORRECT;}
public int getGain(){
//TODO
return 1;
}
public Color getColor(){return blobobj.renderer.material.color; }
public GameObject getBlobObj(){return blobobj;}
//Setters
public void setCORRECT(bool flag){CORRECT = flag;}
public void changeSwipeTo(SWIPE s){Swipe=s;}
}
Thanks.