Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LittleCat1 · Mar 09, 2012 at 12:00 PM · counterplayersdead

Dead Collision

Hi, I've been given this nice bit of code by someone nice on here - and I'm really struggling with it. It's meant to count my players that get the 'dead' variable (which should turn on when they fall off edge and collide with the floor - I've got a feeling it might be global, and not only on the object though, which may need to fix). I also don't really understand the top part, could someone please shed some light on it, please? If someone does rewrite it - UnityScript or C#, it doesn't matter, I've a terrible knowledge of both :D

I'm super new to this, thanks :)

   /* Hooks in said variable count */
  static var dead : int = 0;
  /* Update on each frame */
  function Update() {
  var dead /*Players*/ : int = 0;
  /* Declaring that var i is int (which is 0) */
  for (var i: int = 0)
  // I don't understand at all
  // ++ is add one each time, isn't it?
    i<_players.Length; i++) {
      if (_players[i].dead)
          deadPlayers++;
  }
  /* Counts dead Players - if >=3, do next */
  if (deadPlayers >= 3)
  /*Load Level */
      Application.LoadLevel(1);
  }

&

  // Static Var makes public
  static var dead = false;
  // does the dead tag apply globally? or just to the object that

collided? // Because I want it just on the object

  function OnTriggerEnter (other : Collider) 
  {  
         dead = true;
         Debug.Log (other.gameObject.name); 
  }

TLDR: Can someone explain to me the top part of code/fix it for me? Does the bottom bit of code make the dead variable global, or just on the object collided - How can I change that? Do I need to declare my characters, so they can be counted?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by save · Mar 09, 2012 at 12:19 PM

OnTriggerEnter is only happening on the object it's attached to and only if it has a collider set to trigger.

Static variables can only exist singularly within the namespace. If you want to refer to a static variable you type TheScriptName.dead = true; But I assume that every player can die so it should be a non static variable thus making it available to be different on different objects.

If the trigger is not on the player then you can do like this:

function OnTriggerEnter (other : Collider) {
    if (!other.CompareTag("Player")) return;
    var pScript : PlayerScript = other.GetComponent(PlayerScript); //Name of the player's script
    pScript.dead = true;
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LittleCat1 · Mar 09, 2012 at 12:32 PM 0
Share

I thought static variables could be referenced everywhere :(

So I replace the bottom code of $$anonymous$$e, with yours? and I tag my Players with 'player' tag? $$anonymous$$y top script is called 'dead count' so i reference that in " other.GetComponent(PlayerScript) " - so it needs to be attached to the player?

Sorry, I'm learning. :)

avatar image save · Mar 09, 2012 at 02:14 PM 0
Share

No need to say sorry, it's good you ask questions. :)

You can reference to them from anywhere just as I described, you can only declare them once though. By calling: AnotherScript.aStaticVariable, you reference to a static variable in the other script.

You use GetComponent if the script isn't on the object, for instance if the trigger is a lava lake and the player should emidiately die when entering the trigger. If the script is on the player, variable "dead" needs to be private or regular (non-static) - if you have several players (which you seem to have).

avatar image
0

Answer by LittleCat1 · Mar 09, 2012 at 06:02 PM

I think I'm confusing myself now :(

The on trigger enter is on the 'lava' and searches for tags named player and makes to dead.true if player by using Dead Count.true (name of script is called Dead Count). What's the pscript var about?

Also, I know it's asking an awful lot, but is there any chance you could look at this and see what's happening?

The code you gave me which works (commented out pscript, as I'm not sure it's purpose and it's attached to the 'lava', not a player)

  function OnTriggerEnter (other : Collider) {
      if (!other.CompareTag("Player")) return;
      //var pScript : PlayerScript = other.GetComponent(Dead Count); 
      //Name of the player's script
      DeadCount.DEAD = true;
      Debug.Log ("Dead Count.dead = true;");
  }

and this second script that the first refers to. Everything is commented out so it doesn't give me nasty errors

  //Hooks to other file (DeadCollision) to gain whether dead

or not - i think. static var DEAD = false;

  //Now I need a way to count how many have 'DEAD' tag, and if more than

3 or more, end game. //I have 4 players //need to count the ones with DEAD tag - not sure on code on how to do so.

  //function Update() {
  //var DEAD: int = 0;
  /* Declaring that var i is int (which is 0) */
  //for (var i: int = 0)
  // I don't understand at all
  // ++ is add one each time, isn't it?
    //i<_players.Length; i++) {
     // if (_players[i].dead)
    //      deadPlayers++;
  //}
  /* Counts dead Players - if >=3, do next */
  //if (deadPlayers >= 3)
  /*Load Level */
     // Application.LoadLevel(1);
  //}

Sorry, been doing this like a week now, and issues like these are really stumping me.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Mar 09, 2012 at 06:50 PM 0
Share

Try to use the comments ins$$anonymous$$d of posting an answer to your own question (Quato is a bit different from regular forums). Ok so let's see.. First off name the script DeadCount without spaces.

var DEAD : int = 0; need to be outside Update(), otherwise you'll continuously set that variable to 0 every frame and it'll only be accessible inside Update.

for-loop looks a bit weird. Is it a for-loop you want there?

 //Example of for-loop:
 for (var i : int = 0; i < 4; i++) {
     //Code goes here
 }

You're right, ++ adds to an int each time processed:

 myIntVariable++;


I think you'd be better off with a script that acts as a manager for all your players. Then you could pass a call for a function like so:

 function OnTriggerEnter (other : Collider) {
     if (!other.CompareTag("Player")) return;
     var thePlayerScript : ScriptOnPlayer = other.GetComponent(ScriptOnPlayer); //Get the script on player - Name it accordingly!
     thePlayerScript.kill() //Call a function on the player
 }

 //A part in the thePlayerScript:
 private var dead : boolean = false;
 private var myNumber : int = 0;
 function $$anonymous$$ill () {
     dead = true;
     Game$$anonymous$$anager.$$anonymous$$illPlayer(myNumber);
 }

 //On a script called Game$$anonymous$$anager
 static function $$anonymous$$illPlayer (who : int) {
     //who is now the same int as the player that called it, you can use it as reference for anything
 }

 //The same thing when a player gets added to the game, by calling:
 static var playersAmount : int = 0;
 static function JoinPlayer () {
     playersAmount++;
     return playersAmount;
 }

 So in the player's script you could add:
 function Start () {
     myNumber = Game$$anonymous$$anager.JoinPlayer();
 }

Hope it makes sense, it seems like you're struggling with typing the code right, perhaps you should go through a couple of more tutorials and read through the scripting reference before you throw yourself at this stuff. It takes a bit of time to grasp the concept, so give it time. ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Problems Scripting a Camera 1 Answer

fix sonic like script? 1 Answer

[SOLVED] Enemy Script : Mob doesn't take damage 2 Answers

Destroy gameObject not working? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges