Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by liftedplane · Oct 30, 2017 at 08:08 AM · unity 2dpublic variablepublic static

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Syncing Variables From an Object to a Button 0 Answers

CS0103 and CS0246 with "public Text text;" 0 Answers

Need help with two scrips 0 Answers

Destroying game object 0 Answers

Destroying game object upon trigger issue,Unity 2D destroy player 2 sprite via trigger issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges