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 Taragon · Feb 27, 2011 at 02:11 PM · pickuplerpz3dplatformtutorial

Understanding the Pickup script in Lerpz tutorial

I'm trying to understand one line specifically in the pickup script:
var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus); The reason is because I'm trying to get a GUI box to pop up on other object that Lerpz collides with, not using this script, but using a simple onGUI/onTrigger function to do this. The trigger isn't working (I triple checked the box was checked on the trigger on the interface). I'm not sure why it's not detecting collisions with this onGUI/onTrigger, so I am trying to understand why the pickup script is referencing the thirdpersonstatus... maybe this is why.

Pickup Script:


enum PickupType { Life = 0, FuelCell = 1 } var pickupType = PickupType.FuelCell; var amount = 1; var sound : AudioClip; var soundVolume : float = 2.0;

private var used = false; private var mover : DroppableMover;

function Start () { // do we exist in the level or are we instantiated by an enemy dying? mover = GetComponent(DroppableMover); }

function ApplyPickup (playerStatus : ThirdPersonStatus) { // A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial. switch (pickupType) { case PickupType.Life: playerStatus.AddLife(amount); break;

     case PickupType.FuelCell:
         playerStatus.FoundItem(amount);
         break;
 }

 return true;

}

function OnTriggerEnter (col : Collider) { if(mover && mover.enabled) return; var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);

 //* Make sure we are running into a player
 //* prevent picking up the trigger twice, because destruction
 //  might be delayed until the animation has finished
 if (used || playerStatus == null)
     return;

 if (!ApplyPickup (playerStatus))
     return;

 used = true;

 // Play sound
 if (sound)
     AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);



 // If there is an animation attached.
 // Play it.
 if (animation && animation.clip)
 {
     animation.Play();
     Destroy(gameObject, animation.clip.length);
 }
 else
 {
     Destroy(gameObject);
 }

}

// Auto setup the pickup function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }

@script RequireComponent(SphereCollider) @script AddComponentMenu("Third Person Props/Pickup")

Comment
Add comment · Show 1
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 Bunny83 · Feb 27, 2011 at 04:08 PM 0
Share

Well, wouldn't it be better if you post your script ins$$anonymous$$d of this one? Why do you referring to OnGUI? That have nothing to do with collisions at all. Edit your question if you want to change/add something. I've also fixed your code highlighting. Take a look at the editing help: http://answers.unity3d.com/editing-help And, of course if you haven't already, the FAQs http://answers.unity3d.com/faq

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Feb 27, 2011 at 03:58 PM

The parameter "col" in OnTriggerEnter is the collider that collides with your trigger. This collider is attached to another GameObject. GetComponent search for another Component on this GameObject. You have to tell GetComponent what type of component you're searching for. In this case you want the ThirdPersonStatus script.
The reference that is returned by Getcomponent is stored in "playerStatus".
The line

if ([...] playerStatus == null)

checks whether GetComponent have found a component of this type or not.

With this reference you can access public variables or functions of that ThirdPersonStatus script.
In this case they call some functions of that script:

playerStatus.AddLife()
playerStatus.FoundItem()

I hope that clears it up a bit ;)

Comment
Add comment · 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
0

Answer by Taragon · Feb 27, 2011 at 10:45 PM

I added a cube to the Lerpz world, and onto it the following onGUI/onTrigger functions. Thanks for your help, Bunny83. Please refer to the following: var trigger1: boolean = false;

function OnGUI(){ if (trigger1){ GUI.Label(Rect(0,0,50,50), "Hello",""); } }

function OnTriggerEnter (col : Collider) { Debug.Log(col.name); if (col.name == "Cube") trigger1 = true;

}

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Timer script Update with Pickup 0 Answers

3Dplatformer tutorial GUI script - adding a timer GUI 0 Answers

Unity Platformer Problems 1 Answer

Timer script & game hud 0 Answers

3d Platform tutorial respawn wrong location 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