Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NolanOc · Jan 01, 2018 at 08:47 PM · gameobjectvariablemass

Attaching a "Mass" variable to all or most gameObjects and how to access them

So I'm making a simple game where you move around and eat things smaller than you. I would like to attach a simple variable to game objects to give them a "mass" and check to see that if they are smaller than you then you can eat them etc.

But since each object will have a different mass, how can I add a simple script that I can change depending on who it is on? And then I need to check the players mass against the gameObjects mass.

You don't have to write the script yourself unless I am overcomplicating it, I have no problem reading tutorials I just don't know where to begin.

Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Lysander · Jan 01, 2018 at 09:09 PM

Just make a new MonoBehaviour (called Status or something), add a public float variable to it called "mass", and then set its value in the inspector, or through code. If you're generating objects to eat, then the spawner is likely going to be the object that decides what new objects get what mass value assigned (and there should be a corresponding scale difference between spawned objects to reflect this).

The rest is just a matter of how you consume objects- if it requires a button press, then on button press you can raycast directly ahead of the player, see if there's an object with a "Status" component right in front of you by using GetComponent, and then check the mass value on that component against the player's mass.

Alternatively, you can do it based on trigger areas. This is how Spore works in the "microscopic organism" stage- each mouth on the creature essentially has a small collider (set to IsTrigger) attached. When OnTriggerEnter occurs, the player checks if the colliding object has a "Status" script, and if it does, it'll check its own Status script's mass against the colliding object's Status script's mass, and if it's smaller (not just smaller, but like 20% smaller), proceed to damage it, or eat it if it's health is low enough (health can also be stored in the same script). There's an additional check to see if the "type" is appropriate, since certain mouths can only eat plant objects, certain mouths can only eat meat objects, and certain mouths can eat both.

All of this is fairly straightforward- just go to the Unity Learn site and watch the basic tutorials on MonoBehaviours / Components, Colliders, and Triggers. I would personally go though the entire "Interface and Essentials" section and the beginner section for "Scripting" before even opening up Unity (except to follow along). It only takes a couple of hours, and it will make your experience with Unity FAR less stressful.

Hope that helps!

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 NolanOc · Jan 02, 2018 at 04:15 PM 0
Share

Very helpful thanks a lot!

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

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Decrease variable after pressing a button 1 Answer

shrinking objects 1 Answer

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

How to save a gameObjects on a prefab and variables with a script? 1 Answer

Destroying object destroyable? 1 Answer


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