triadakiss.blogg.se

Reference a script unity by calling the script it inhjerits
Reference a script unity by calling the script it inhjerits













  1. Reference a script unity by calling the script it inhjerits upgrade#
  2. Reference a script unity by calling the script it inhjerits code#

You could just add a method which has a return value of float which then always returns the variable of speed. note you won't be able to change the variable as the setter is private To then access the speed variable you simply get it from the instance of the class: move.speed Since this is then not the same as speed you need to set it at startup. While automatic setters and getters are awesome for variables you want to share between classes/scripts the inspector won't allow you to use them (which is why i assume speed is public) so I added an extra float which is visible to the inspector. So you might notice this is a lot of code, Yes. Which you might want so you don't change it be accident.

Reference a script unity by calling the script it inhjerits code#

There are many ways that you can do this:īy changing the code to the code below you are making the variable available to the outside classes (public get ) but those classes can't change the variable. Basically it is a way of getting and/or setting variables in classes (or scripts in this case) from external code. Spawn = ("Spawn") įlame = ("Flame").What you need are getters and setters. Public float minPrecision, maxPrecision, minReload, maxReload Ĭylinder = ("Cylinder") Private Transform cannon, cylinder, spawn, closestEnemy Update1: Added BaseCannon.cs: public class BaseCannon : BaseWeapon

reference a script unity by calling the script it inhjerits

Thank you for looking into this I appreciate it a lot as ive been trying to figure this out for almost 2 days now. These last 2 scripts work fine, the problem doesnt seem to be there. An Audio Source will be attached to the GameObject in the inspector. You can search for Audio Source and select this. With the new GameObject selected in the inspector, click Add Component. Go to GameObject and click Create Empty from the menu. IDoUpgrade.cs interface public interface IDoUpgradeĬalling TryDoUpgrade() baseCannon = To create a new Audio Source: Import your audio files into your Unity Project. I used the exact same interface structure to do damage, as all cannons need to do damage, but again with different params, in that structure I have no issues, but as I recreated it for Upgrades it doesnt seem to work but what makes this so confusing is that there are no console errors, no missing references, it debugs function A that then calls function B, but the debug in function B then never gets called! Var baseDamage = baseCannon.damage / baseCannon.level īaseCannon.damageType = new DefDamage(lvl) //CHECK IF IT WORKSīaseCannon.minPrecision = DefUpgradeDictionary.minPrecision īaseCannon.maxPrecision = DefUpgradeDictionary.maxPrecision īaseCannon.minReload = DefUpgradeDictionary.minReload īaseCannon.maxReload = DefUpgradeDictionary.maxReload

reference a script unity by calling the script it inhjerits

Public void DoUpgrade(BaseCannon baseCannon)ĭebug.Log("Inside DoUpgrade SUCCESS") // This debug is never reached/called Public DefUpgrade(BaseCannon baseCannon, int level)īst = st To clarify, the above script is only used to set the parameters, I have a prefabCannon, with no scripts, when I instatiate that prefabCannon, I add the script DefCannon or FireCannon, depending on which cannon I want to Instantiate, this way I can have them all use the generic functions they would all need, but have custom paramters and such for each one as well.ĭefUpgrade.cs This is were the problem is, the Debug.Log in the BaseWeapon script works, but the next step it takes when it enters into DefUpgrade.cs and tries to call DoUpgrade() fails, because there Debug.Log in that script is never called: public class DefUpgrade : IDoUpgrade However DefUpgrade(BaseCannon baseCannon, int level) DefUpgrade is asking for a BaseCannon, instead we give it a child of BaseCannon in this case that is DefCannon, might this be the reason why its not working? and how would I solve this, I dont have a reference to the BaseCannon script from the child script, is there a way to do this.BaseCannon, (calling the parent class somehow?)

reference a script unity by calling the script it inhjerits

StudioEventEmitter::PlayEvent The Unity Event used to trigger the creation and starting of the Event Instance. I think the problem might be here aswell upgradeType = new DefUpgrade(this, level) this = DefCannon.cs // which inherits from BaseCannon This component inherits from EventHandler. UpgradeType = new DefUpgrade(this, level)

reference a script unity by calling the script it inhjerits

The above debug works, and gold is subtracted from the player as expected.ĭefCannon.cs I use this to instantiate a BaseCannon that inherits from BaseWeapon, with custom parameters, this is the default cannon, I also have a FireCannon as an example, they all inherit from BaseCannon, which in turn inherits from BaseWeapon.: public class DefCannon : BaseCannonĬannonMat = Cannonballs.cbCannonMat ĬbSpeed = Cannonballs.cbSpeed Public void TryDoUpgrade(BaseCannon cannon)ĭebug.Log("Inside TryDoUpgrade SUCCESS") // This debug works Int dmg = (int)damageType?.DoDamage(target, damage) Public int TryDoDamage(BaseEnemy target, int damage)

Reference a script unity by calling the script it inhjerits upgrade#

I am trying to upgrade a specific cannon, by using an interface, the problem I am facing is that for some reason the functions are being called correctly and there are no errors, but in fact the parameteres are not "upgraded" as expected.















Reference a script unity by calling the script it inhjerits