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 /
avatar image
0
Question by TBaker36 · Sep 13, 2019 at 03:29 PM · editorvariablestools

How do I make a tool that allows me to modify variables in scripts that are not MonoBehaviour?

I made some power ups for a game and they are derived from the base PowerUp class. They work how I want them too but I want to be able to modify their variables during run time (i.e. effect duration, blast radius...). Is there a way to do this already, or can someone point me in the right direction to make a tool that can do this.

Thank you in advance!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PowerUp 
 {
     public bool hasDuration;
     public float duration;
     public float radius;
 
     public PowerUp(bool _hasDuration, float _duration, float _radius)
     {
         hasDuration = _hasDuration;
         duration = _duration;
         radius = _radius;
 
     }
 
     public virtual void CheckForDuration()
     {
 
     }
 
 
     public virtual void ActivatePowerUp()
     {
    
     }
 
     public virtual void ResetEffects()
     {
 
     }
 }

Above is the base class and here is a sample of one of the Power ups

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GottaGoFast : PowerUp
 { 
     private float playerStartSpeed;
     private float speedMultiplier;
 
     public GottaGoFast(bool _hasDuration, float _duration, float _radius, float _speedMultiplier) : base(_hasDuration, _duration, _radius)
     {
         speedMultiplier = _speedMultiplier;
     }
 
     public override void CheckForDuration()
     {
         base.CheckForDuration();
         hasDuration = true;
         duration = 5f;
         var player = GameObject.FindGameObjectWithTag("Player").GetComponent<JoystickPlayerExample>();
         playerStartSpeed = player.speed;
     }
 
 
     public override void ActivatePowerUp()
     {
         base.ActivatePowerUp();
 
         var player = GameObject.FindGameObjectWithTag("Player").GetComponent<JoystickPlayerExample>();
         player.speed = player.speed + (player.speed * speedMultiplier);
 
         Debug.Log("Gotta go fast! Power Up Used!");
 
     }
 
 
     public override void ResetEffects()
     {
         base.ResetEffects();
 
         var player = GameObject.FindGameObjectWithTag("Player").GetComponent<JoystickPlayerExample>();
         player.speed = playerStartSpeed;
     }
 
 }

Right now I am using constructors that will get values from a scriptable object but i would like to know if i could make a tool to do this instead or a good starting point as to how i could go about it.

Comment
Add comment · Show 1
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
avatar image Bonfire-Boy · Sep 13, 2019 at 05:36 PM 0
Share

I'm not quite sure what you're getting at. You can modify the variables directly if they're public, otherwise you need some kind of accessor functions/properties. That's just basic C#, you certainly don't need a "tool".

So I suspect the issue is something other than than what you've stated. Can you elaborate? Like, how are you creating and accessing these things? Since they're not serialised, presumably you've got some other object that's creating them at runtime and providing access to them...?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TBaker36 · Sep 16, 2019 at 03:30 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PowerUpBox : MonoBehaviour
 {
     public PowerUp[] powerups;
     public PowerUpEditor PUE;
 
     private bool isDisabled;
 
     [SerializeField]
     private float disableTimerStart = 5f;
 
     private float disableTimer;
 
     public int itemNumber;
 
 
     private GameObject player;
 
 
     private void Start()
     {
         powerups = new PowerUp[9];
 
         powerups[0] = new BlastZone(PUE.BZ_hasDuration, PUE.BZ_duration , PUE.BZ_radius, PUE.BZ_power, PUE.BZ_upwardForce);
         powerups[1] = new Chillout(PUE.CO_hasDuration, PUE.CO_duration, PUE.CO_radius);
         powerups[2] = new GottaGoFast(PUE.GF_hasDuration, PUE.GF_duration, PUE.GF_radius, PUE.GF_speedMultiplier);
         powerups[3] = new CantTouchThis(PUE.CTT_hasDuration, PUE.CTT_duration, PUE.CTT_radius);
         powerups[4] = new SneakySnake(PUE.SS_hasDuration, PUE.SS_duration, PUE.SS_radius);
         powerups[5] = new Thiccness(PUE.TH_hasDuration, PUE.TH_duration, PUE.TH_radius);
         powerups[6] = new BallsOfSteel(PUE.BS_hasDuration, PUE.BS_duration, PUE.BS_radius);
         powerups[7] = new ISeeDeadPeople(PUE.IS_hasDuration, PUE.IS_duration, PUE.IS_radius);
         powerups[8] = new CalmDown(PUE.CD_hasDuration, PUE.CD_duration, PUE.CD_radius, PUE.CD_speedMultiplier);
 
         disableTimer = disableTimerStart;
     }
 
 
     private void Update()
     {
        
         if (isDisabled)
         {
             disableTimer -= Time.deltaTime;
         }
         if (disableTimer <= 0)
         {
             EnablePowerUp();
         }
 
     }
 
 
     private void OnTriggerEnter(Collider other)
     {
         var _powerUpTracker = other.gameObject.GetComponent<PowerUpTracker>();
 
         if (other.gameObject.tag == "Player")
         {
 
             if (_powerUpTracker.slot1 == null)
             {
                 //_powerUpTracker.slot1 = powerups[1];
                 _powerUpTracker.slot1 = powerups[Random.Range(0, powerups.Length)] ;
                 DisablePowerUp();
                 _powerUpTracker.UpdateUI();
                 return;
             }
             if (_powerUpTracker.slot2 == null)
             {
                 _powerUpTracker.slot2 = powerups[5];
                 //_powerUpTracker.slot2 = powerups[Random.Range(0, powerups.Length)];
                 DisablePowerUp();
                 _powerUpTracker.UpdateUI();
                 return;
             }
             
         }
     }
 
     void DisablePowerUp()
     {
         isDisabled = true;
         gameObject.GetComponent<MeshRenderer>().enabled = false;
         gameObject.GetComponent<Collider>().enabled = false;
     }
 
     void EnablePowerUp()
     {
         isDisabled = false;
         disableTimer = disableTimerStart;
         gameObject.GetComponent<MeshRenderer>().enabled = true;
         gameObject.GetComponent<Collider>().enabled = true;
     }
 
 
 }

So I made an Item box that knows about all the power ups but their scripts aren't attached to anything. Since they aren't attached to anything I can't edit their variables during run time. I am not sure if I worded it wrong or what, but does that make more sense. I also provided the script I am using for the item boxes.

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

148 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 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

Changing collider's center changes the position of the position handle? wtf?! 1 Answer

Unable to find java in the system path 3 Answers

Saving editor-only variables 0 Answers

Get variables from an editor script through a regular script 1 Answer

What are the editor resources by name for EditorGUIUtility.Load Method? 6 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