c# - Acessing custom property with AddComponent -
in unity3d i've got script adds variable 'eaten' component.
using unityengine; [addcomponentmenu("my game/iseaten")] public class iseaten : monobehaviour { public bool eaten; }
yay! can add script access 'eaten'
using unityengine; using system.collections; public class test : monobehaviour { private eaten somescript; // use initialization void start () { somescript = getcomponent<iseaten>(); bool temp = somescript.eaten; print(temp); // false } }
which works fine. have access variable dot notation script? ie
if (mycube.eaten == true) { // }
you know, in unity 1 create whole script add single property object. common approach think of scripts 'components' (which are, in fact). let me explain this, component single piece of code add functionality gameobject
, ability animate, or behave laws of physics. so, maybe, better reform iseaten
class form true component, pickup
(i'm assuming need eaten
property pickup of sort) have functionality eaten player, or something.
// need attach collider (and check 'istrigger' checkbox on it) gameobject // method called whenewer game object collider (e. g. player, npc's, monsters) enter trigger of object void ontriggerenter(collider other) { // check whether object eaten before , if actor entering our trigger player if (!eaten && other.tag == "player") { // make shure not eaten twice eaten = true; // apply effect player has devoured other.getcomponent<player>().addhp(25); } }
other that, i'm thinking, getting out of way enable little sweeter syntax not worth hassle, but, if provide insight of trying implement, may try :)
Comments
Post a Comment