- Home /
Enter Class(method) with a script, attached to another Object
The following text is in German. For English you need to read below.
Ich mache grade ein Schulprojekt in Informatik und habe nun folgendes Problem. Eine kleine Kugel, die vom Spieler abgeschossen wird, trifft einen anderen (bzw. getaggten) Spieler.
if(col.tag == "Player")
{
Component colPlayerAttributes = col.gameObject.GetComponent("PlayerAttributes");
colPlayerAttributes.DealDamage(10);
}
Ich möchte jetzt auf eine Klasse, die jedem Spieler zugewiesen ist zugreifen, und die darin enthaltenen Lebenspunkte ändern. Mein problem ist, wie schaffe ich es, auf das Klasse zuzugreifen?
Falls ihr noch irgendwas braucht, sagt bescheid! Vielen Dank im voraus!
Google Translate:
I'm just a school project in computer science and now I have the following problem. A small ball that is fired by the player hits another (or tagged) players.
I now want to access a class that is assigned to each player, and change the life points contained therein. My problem is how can I get access to the class?
[If you had any advice, I'll take it]
Thanks in advance!
Answer by perchik · Mar 06, 2014 at 09:43 PM
In C#, die "GetComponent" syntax ist anders
PlayerAttributes colPlayerAttributes = col.gameObject.GetComponent<PlayerAttributes>();
if(colPlayerAttributes == null){
Debug.LogError("es gibt keine 'PlayerAttributes' Komponente befestigt 'col'")
}
Answer by nesis · Mar 06, 2014 at 09:44 PM
You were close. Use this:
PlayerAttributes colPlayerAttributes = col.GameObject.GetComponent<PlayerAttributes>();