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
1
Question by lmstam · Sep 29, 2015 at 07:56 PM · objectsvaluesimplementation

Multiple objects, same script, different values

I'm trying to make a farm game where it's possible to plant three different kinds of plants. They all act the same but they have different values (for example: they take longer/shorter to grow, different sprites, give more money etc.) Would I be able to implement this in the same script? That when a certain button is clicked, the fitting values for that kind of plant will be added to the script? Hope you can help me.

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
4

Answer by MerryAce123 · Sep 29, 2015 at 09:05 PM

This is actually what public variables do for you. On the start of your script you declare your variables which will have your changable values. Then you just work with the variables for the rest of the script. When you assign the script to multiple objects you can just set those values from the inspector or you can make some interactions inside the game which will set the values for you. Here is an example:

 public float growTime;
 public float size;
 public float price;
 // whatever else you need...
 
 void Update(){
 
 //And here you just work with your variables instead of specific numbers
 
 }

If you want to set your variables from somewhere else, like you click a button or press a key you just create your plant and assign the values by using GetComponent... Here is an example how something like a "Farm manager" could look like:

 public GameObject plant;
 
 void Update() {
 
 if(Input.GetKeyDown("s")){
 
 GameObject g = Instantiate(plant, transform.position, Quaternion.identity) as GameObject;
 g.GetComponent<TheNameOfYourPlantScript>().growTime = 5;
 
 }
 
 
 }
 

You create a prefab and put your plant object inside. Then you assign that prefab into the plant variable which will store the prefab. The if certain action happens like you press or click something we instantiate it, you can swap that transform.position for any Vector3 and then the instantiated object is stored into a "g" variable. The we take the plant script from our "g" variable and fill in any value we want.

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 lmstam · Sep 30, 2015 at 06:31 AM 0
Share

Thank you! I'll try this out later. :)

avatar image GiyomuGames · Sep 30, 2015 at 07:54 AM 0
Share

That is definitely the right way to do it :)

avatar image
0

Answer by Krunal_vasundhara · Jul 24, 2017 at 05:37 AM

I am use it but it still shows the null exception error in unity.

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

7 People are following this question.

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

Related Questions

edit values for multiple objects concurrently 1 Answer

Same script on multiple objects. 2 Answers

how to pass values between objects in C# 2 Answers

Change materials of connecting objects 0 Answers

Swapping Objects 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