Question by
Ranimo1 · Apr 04, 2020 at 12:15 AM ·
scripting problemobject reference
Object Reference error when no object is being referenced.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collectables : MonoBehaviour {
private Laser laser;
private Player player;
private Leveling level;
public GameObject enemy;
private void Start()
{
level = GetComponent<Leveling>();
laser = GetComponent<Laser>();
player = GetComponent<Player>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
Instantiate(enemy, new Vector2(Random.Range(-2, 3), Random.Range(5, 10)), Quaternion.identity);
Destroy(this.gameObject);
}
else if (collision.tag == "Enemy" && this.tag == "Laser")
{
Instantiate(enemy, new Vector2(Random.Range(-2, 3), Random.Range(5, 10)), Quaternion.identity);
level.IncreaseScore();
Destroy(collision.gameObject);
Destroy(this.gameObject);
}
else if (collision.tag == "Enemy" && this.tag == "Player")
{
player.playerHealth.Decrement();
}
else if (collision.tag == "Shield" && this.tag == "Enemy")
{
Instantiate(enemy, new Vector2(Random.Range(-2, 3), Random.Range(5, 10)), Quaternion.identity);
Destroy(collision.gameObject);
Destroy(this.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Leveling : MonoBehaviour {
public Text text;
private int level;
private int score;
private int scoreMax;
private int hp;
// Start is called before the first frame update
// Update is called once per frame
void Start()
{
level = 1;
score = 0;
scoreMax = 50;
hp = 3;
}
void IncreaseLevel()
{
level++;
}
public int getHP()
{
return hp;
}
public void IncreaseScore()
{
score = score + 10;
}
void Update()
{
if (score >= scoreMax)
{
score = 0;
scoreMax = scoreMax + 100;
IncreaseLevel();
}
text.text = "Level: " + level;
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make a character controller through another script 0 Answers
Trying to track the BoxColliders of 2 Different Sets of Instantiated GameObjects 1 Answer
ObjectReference Not set to an instance of an object 1 Answer
How do I set value ScriptableObject list element property in script? 1 Answer
Isometric Movent Issue 0 Answers