- Home /
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.
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...?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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