c# - Stealing enemies points in multiplayer game -
i have "player" gameobject
spawn onserverinitialized()
. it's tag "enemy" change "player" when getcomponent<networkview>().ismine
.
i'd make like:
void ontriggerenter (collider enemy){ if (scoremanager.score > enemy.score) { scoremanager.score = scoremanager.score + enemy.score; } else if (scoremanager.score < enemy.score) { destroy (gameobject); } }
but don't know how access spawned enemy player's points.
my scoremanager script:
public static int score; text text; void awake () { text = getcomponent <text> (); score = 0; } void update () { text.text = "score: " + score; } }
it attached gui text gameobject
named scoretext
.
1)when you're using getcomponent check null.
2)don't use getcomponent. serialize variable (or make public) addding [system.serializable]
above text text;
, drag , drop in inspector, text component.
3)you don't have refresh score every frame, when changes. should done using events.
in order access enemy's points, can use gameobject.findwithtag, , use "enemy" provided enemy has tag. can reference enemy game object way. access score component.
for example:
scoremanager enemyscoremanager = null; gameobject enemyreference = gameobject.findwithtag("enemy"); if (enemyreference !=null) enemyscoremanager = enemyreference.getcomponent<scoremanager>(); if (enemyscoremanager != null) { enemyscoremanager.score -=5; //steal 5 points myscore.score +=5; //assuming myscore reference score manager } else debug.logerror("enemy not found");
Comments
Post a Comment