Clickable object that increases mana variable in second script
I've spent all day trying to figure this out and while I'm much closer then I was at the beginning I'm stuck
Here's the problem I'm having,
Towers instantiate mana powerups every so often, when you click on this object it adds to your mana pool and then destroys itself.
I have everything working except adding to the mana pool. I've tried making mana a static variable, I've attempted to make the entire class for manaAdd static and it broke even more.
the current script is the closest to working I've gotten it, the problem is it still won't increase the mana value even though it calls and uses the funtion and destroys the object.
Mana Adding object that is in the level this script is attached to an empty game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ManaAdd : MonoBehaviour {
public int manaToAdd = 5;
public static int mana = 100;
Text TotalMana;
private void Start()
{
InvokeRepeating("addMana", 5, 5);
TotalMana = GetComponent<Text>();
}
private void LateUpdate()
{
TotalMana.text = "Mana: " + mana;
SetMana();
}
private int SetMana()
{
mana += DestroyPowerUp.passMana;
return mana;
}
private void addMana()
{
mana += manaToAdd;
}
}
this is the script attached to towers that instantiates a clone of a prefab that floats and disappears after a short amount of time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MagicSpawn : MonoBehaviour {
//public variable for theprefab
public GameObject magicPrefab;
private Rigidbody2D magicRb;
private void Start()
{
magicRb = magicPrefab.GetComponent<Rigidbody2D>();
//Random range min inclusive, max exclusive.
InvokeRepeating("spawnMagic", (Random.Range(6, 11)), (Random.Range(10, 35)));
}
void spawnMagic()
{
//load prefab transform.position to find current position quaternion.identity def rotation
Instantiate(magicPrefab, transform.position + transform.right * (Random.Range(1,9)), Quaternion.identity);
}
}
This code moves and destroys the powerup. I also want to be able to click the powerup and have it increase the amount of mana available from the first script. I cannot figure out what I'm doing wrong and I've tried various methods, this is the closest that's worked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyPowerUp : MonoBehaviour {
[SerializeField]
public GameObject manaAdder;
private Vector2 floatSpeed;
private int manaAdded = Random.Range(1, 8);
private int currentMana;
public static int passMana;
private void Start()
{
manaAdder = GameObject.FindGameObjectWithTag("ManaAdder");
floatSpeed.y = (Random.Range(0.05f, 0.4f));
}
private int GetMana()
{
currentMana = ManaAdd.mana;
return currentMana;
}
private int PassAddedMana()
{
passMana += manaAdded;
return passMana;
}
private void OnMouseDown()
{
PassAddedMana();
Destroy(gameObject);
}
private void LateUpdate()
{
GetComponent<Rigidbody2D>().velocity = floatSpeed;
Destroy(gameObject, 9.0f);
GetMana();
passMana = currentMana;
}
}
I'm willing to refactor whatever I have to as I've already written this a few different ways and none of them are working, I actually wanted the magic spawn to also handle the magic destruction, however because the object were instantiated as clone objects I couldn't get the tower code to destroy the cloned objects, this seemed to be what most google searches said to do, so I figured why not also have it move and add to mana when you click on it.
Answer by liftedplane · Oct 30, 2017 at 11:40 PM
am I allowed to answer my own question?
here's what I've done, I've placed click handling inside the main manaAdd script, it now creates a ray to the mouse position on click, gathers the hit and tells what the object is, if it's the proper object (a mana ball powerup) it the runs the powerupadd function which randomly increases the mana in a range from 1-7 (8 is exclusive in the code)
then it figures out which object was clicked and deletes it.
here's the scripts I've cleaned them up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ManaAdd : MonoBehaviour {
public int manaToAdd = 5;
public static int mana = 100;
public Text TotalMana;
private int pUpMana;
private Vector3 manaPos;
private Vector2 findMana;
private RaycastHit2D hitMana;
private void Start()
{
InvokeRepeating("addMana", 5, 5);
TotalMana = GetComponent<Text>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
manaPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
findMana = new Vector2(manaPos.x, manaPos.y);
hitMana = Physics2D.Raycast(findMana, Vector2.zero);
if (hitMana.collider.name == "Magic(Clone)")
{
Debug.Log("This is a " + hitMana.collider.gameObject.name);
addPowerUpMana();
}
else
{
Debug.Log("Try Again Stupid");
}
}
}
private void LateUpdate()
{
TotalMana.text = "Mana: " + mana;
}
private void addMana()
{
mana += manaToAdd;
}
private void addPowerUpMana()
{
pUpMana = Random.Range(1, 8);
mana += pUpMana;
DestroyObject(hitMana.collider.gameObject);
}
}
and here's the destroy and move onject script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyPowerUp : MonoBehaviour {
private Vector2 floatSpeed;
private void Start()
{
floatSpeed.y = (Random.Range(0.05f, 0.4f));
}
public void destroyMana()
{
Destroy(gameObject);
}
private void LateUpdate()
{
GetComponent<Rigidbody2D>().velocity = floatSpeed;
Destroy(gameObject, 9.0f);
}
}