I have the following situation:
I use Unity Playground to create some sort of Asteroids for practicing my unity skills, which realy suck a lot.
I followed the “How to Start Making Games - Unity Playground (Beginner Friendly)” instructions so far and replicated the identical concept. There is a script called “DestroyForPointsAttribute.cs”
I edited it like follows. (more on that below)
using UnityEngine;
using System.Collections;
[AddComponentMenu("Playground/Attributes/Destroy For Points")]
public class DestroyForPointsAttribute : MonoBehaviour
{
public int pointsWorth = 5;
private UIScript userInterface;
private int this.gameObject.healthPoints = 15;
private void Start()
{
// Find the UI in the scene and store a reference for later use
userInterface = GameObject.FindObjectOfType<UIScript>();
}
//This will create a dialog window asking for which dialog to add
private void Reset()
{
Utils.Collider2DDialogWindow(this.gameObject, false);
}
//duplication of the following function to accomodate both trigger and non-trigger Colliders
private void OnCollisionEnter2D(Collision2D collisionData)
{
OnTriggerEnter2D(collisionData.collider);
}
// This function gets called everytime this object collides with another trigger
private void OnTriggerEnter2D(Collider2D collisionData)
{
// is the other object a Bullet?
if(collisionData.gameObject.CompareTag("Bullet"))
{
if(this.gameObject.healthPoints == null)
{
this.gameObject.healthPoints = 15;
}
this.gameObject.healthPoints -= 5;
if (this.gameObject.healthPoints < 1)
{
if(userInterface != null)
{
// add one point
BulletAttribute b = collisionData.gameObject.GetComponent<BulletAttribute>();
if(b != null)
{
userInterface.AddPoints(b.playerId, pointsWorth);
}
else
{
Debug.Log("Use a BulletAttribute on one of the objects involved in the collision if you want one of the players to receive points for destroying the target.");
}
}
else
{
Debug.Log("There is no UI in the scene, hence points can't be displayed.");
}
// then destroy this object
Destroy(gameObject);
}
}
}
}
I tried:
private int this.gameObject.healthPoints = 15;
private int gameObject.healthPoints = 15;
public >the rest = the same<
As you already can see, I cannot wrap arount my head around the structure of this stuff. C# is killing me. This script is directly linked to the asteroid.
Usually the script will just destroy asteroid and the bullet stays and it gives the player 1 point or what ever was configured.
I want the object to have health, but somehow I cannot set the variable to the object (asteroid).
I do not realy get it. Why can I not just use those variables?