Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by DelusionalDel · May 09, 2020 at 08:15 AM · scripting beginnermultiple objects

Using one script for multiple objects

I have the same script on a few objects but the values change on all objects.enter code here

 void Update()
     {
         daysPassed = Calendar.GetComponent<Calendar>().DaysPassed;
         if(daysPassed > 3)
         {
             if (watered == "yes")
             {
                 GetComponent<MeshFilter>().mesh = sunflower2;
                 planted = false;
             }
             else
             {
                 daysPassed = 0;
                 planted = false;
                 GetComponent<MeshFilter>().mesh = sunflower1;
                 GetComponent<MeshRenderer>().enabled = false;
             }
         }
     }

the dayspassed is set to 0 when planted, but it sets all plants dayspassed to zero

 void StartCycle()
     {
         //Calendar.a = 0;
         Calendar.GetComponent<Calendar>().DaysPassed = 0;
         Debug.Log(daysPassed);
     }

Comment
Add comment · Show 4
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 lukis97 · May 09, 2020 at 09:29 AM 1
Share

$$anonymous$$y best guess is that they are all working of off the same instance of Calendar. Would you $$anonymous$$d showing what your calendar obejct looks like, and which gameobjects hold which instances?

avatar image DelusionalDel lukis97 · May 09, 2020 at 07:14 PM 0
Share

@lukis97 Yea they are all using the same calendar. i didnt know i couldnt do that.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using T$$anonymous$$Pro;
 
 public class Calendar : $$anonymous$$onoBehaviour
 {
     float year = 1;
     public static float day = 1;
     string[] season = {"Spring", "Summer", "Autumn", "Winter"};
     int i = 0;
     public static int a = 0;
     public float DaysPassed = 0;
     PlantControl pC;
 
     private void Awake()
     {
         //comment this out and uncomment "a++;" in UpdateDay()
         //PlantControl daysPasssed = pC.GetComponent<PlantControl>();
         //plant = daysPasssed.daysToNextCycle;
     }
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if(TimeInfo.hour > 23 && TimeInfo.$$anonymous$$ > 5)
         {
             UpdateDay();
         }
         if(day > 30)
         {
             if(i < 3)
             {
                 i++;
             }
             else
             {
                 i = 0;
                 year++;
             }
             day = 1;
         }
         
         GetComponent<Text$$anonymous$$eshProUGUI>().text = day + " " + season[i] + " " + "year: " + year;
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             UpdateDay();
         }
     }
     void UpdateDay()
     {
         day++;
         //a++;
         DaysPassed++;
         Debug.Log("New Day: " + DaysPassed);
     }
 }
 
avatar image lukis97 DelusionalDel · May 09, 2020 at 10:24 PM 0
Share

It looks like plants should keep track of which day they were planted, and calendar should keep track of how many days that have passed since the start of the program. That would reduce the amount of components and give you more flexibility with future plants.

This would mean that "daysPassed" is "Calendar.GetComponent().GetDaysSinceStart() - dayPlanted". Don't forget to calculate the amount of days while taking year+month in account.

Good luck!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

209 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 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

Best Script to create a domino lighting effect of objs around player (game obj light ripple) 0 Answers

How can I hide a part of a mesh at runtime? 0 Answers

Why Does this not work? please someone help me i am new to coding. 1 Answer

I need Function to be in other script 1 Answer

Calling function to update variable from prefab script not working as intended 0 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