- Home /
Creating a game similar to ant smasher? Error
Hi there, This is what I have done so far.
It does not seem to be working.. any advice?
1)Create a sphere and make it as a prefab.Rename it as enemy.
2)Create an Empty GameObject and rename it as LevelHandler.
3)Create an Empty GameObject and rename it as ScoreHandler.
4)create a script for enemy and add the following code. Drag the script to enemy and apply.
#pragma strict
var speed:float;
var effect:GameObject;
var scorehandler:GameObject;
function Awake () {
scorehandler=GameObject.Find("ScoreHandler");
var temp:float=LevelHandler.level;
Debug.Log(temp);
speed=temp/10;
}
function Update () {
transform.Translate(0,0,speed*Time.deltaTime);
}
function OnMouseDown()
{
scorehandler.gameObject.SendMessage("AddScore");
Instantiate(effect,transform.position,transform.rotation);
Destroy(gameObject);
}
5)create a script for levelhandler add the following code. Drag the script to LevelHandler in hierarchy
#pragma strict
public static var level:int=1;
var enemy:GameObject;
function Start () {
level=1;
InvokeRepeating("Bug",1.0,1.0);
}
function Update () {
}
function Bug()
{
var pos=Random.Range(-1.0,1.0);
Instantiate(enemy,Vector3(pos,0,1),transform.rotation);
}
function StopBug()
{
CancelInvoke("Bug");
var obj=GameObject.FindGameObjectsWithTag("Finish");
for(var i=0;i<obj.length;i++)
{
Destroy(obj[i].gameObject);
}
}
function IncreaseLevel()
{
level+=1;
yield WaitForSeconds(2);
InvokeRepeating("Bug",1.0,1.0);
scorehandler.gameObject.BroadcastMessage("LevelNo",level);
}
6)create a script (C#)for scorehandler add the following code. Drag the script to ScoreHandler in hierarchy
using UnityEngine;
using System.Collections;
public class ScoreHandler : MonoBehaviour {
public int[] tlevel;
public int level;
public int score;
public int bug=0;
private GameObject LevelHandler;
// Use this for initialization
void Start () {
LevelHandler = GameObject.Find ("LevelHandler");
level = 1;
}
// Update is called once per frame
void Update () {
}
void AddScore()
{
for (int i=0; i<tlevel.Length; i++) {
if (level == tlevel [i]+1) {
if (bug >= 10) {
LevelHandler.gameObject.SendMessage ("StopBug");
LevelHandler.gameObject.SendMessage ("IncreaseLevel");
bug = 0;
}
}
}
bug += 1;
score += 10;
}
void OnGUI()
{
GUI.Label (new Rect (Screen.width - 100, 25, 100, 100), "Score " + score);
}
void LevelNo(int lev)
{
level = lev;
}
}
7)Place enemy prefab to enemy in hierarchy for LevelHandler script
8)Change tlevel size to 10 in scorehandler script in inspector and give values from 0,1,2,,,...to 9.
I keep getting error "unknown identifier: scorehandler" from the levelhandler script.
What am i doing wrong?.. any help is appreciated.
From what I can see you're using scorehandler reference in line 41 of your "LevelHandler" script - but it doesn't contain a reference to scorehandler instance. Either find it like you do in your ScoreHandler finding LevelHandler, or provide it in Inspector like you do with your enemy prefab.
You don't have a variable scorehandler in your LevelHandler class. You would have to use GameObject.Find("ScoreHandler").GetComponent() to get a reference to scorehandler, but since you are mixing javascript and c#, it won't be as simple as that. Can't remember right away how it should be done in this case.
Your answer
Follow this Question
Related Questions
first tutorial to use? 1 Answer
My IOS touch input is not working 0 Answers
Why my object not disappear? 1 Answer
How do I make a level-to-level loading screen with bar? 1 Answer
Addforce.forward on a sphere object 1 Answer