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);
}
$$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?
@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);
}
}
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!
Your answer
Follow this Question
Related Questions
Script Coding From One Scene To Another 0 Answers
How can i run a script with a single key press? 1 Answer
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