Hello =)
this is my first real post so I hope it´s alright.
One goal of my game is to shoot 12 objects in a specific order.
At the start, when any one object - e.g. Object A - gets shot by the player, it will change its color to green.
At the same time, two more objects will change their color, in this case to blue:
Object B: The object that needs to be shot next.
Object C: One of the other 10 objects, that will be randomly picked in order to mislead the player a little.
If the player now shoots the right object, Object B, it will also turn green, and Object C will return to its default color. Now, the object that has to be shot next (overwriting Object B), and a newly selected random object (overwriting Object C) will turn blue. So on and so forth.
If the player shoots the random Object, he fails, meaning all Objects will return to its default color and the player can shoot any object two start the riddle again.
The Problem I have, is overwriting Object C (randomGuardian in the script). It seems to be not overwritten properly.
Instead, Object C seems to have a reference to the old AND the new Object that it was/is referenced to. I debugged pretty much anything and all the logs where as expected.
The old randomObject gets indeed unhighlighted but it gets immediatly highlighted again when
looping through the “guardiansToHighlight”. At this point it should have been overwritten.
And the newly assigned randomObject also gets highlighted. So there must be a one reference to two objects right? But that doens´t make sense to me.
Here is the code with comments. I am pretty new to Unity so it´s not very elegant.
I would appreciate any help.
Thanks =)
// Gets called everytime player shoots an object
void AdvanceActivationIndex(GameObject shotObject)
{
// On Default activationIndex = -1
if(activationIndex == -1)
{
// Find the index of the shot Object in a given "patternList" of 12 GameObjects and store it as "activationIndex"
activationIndex = patternList.IndexOf(shotObject);
// make a "newPatternList" and populate it, where shotObject is at index 0 and the rest of the objects follow in the same order as in patternlist
newPatternList = new List<GameObject>();
for(int i = 0; i < 12; i++)
{
newPatternList.Add(patternList[activationIndex]);
activationIndex++;
// Make Sure Index is inside the bounds of the list
if (activationIndex > 11)
activationIndex = 0;
}
// Reset activationindex, which will be used to determine which objects should be shot
activationIndex = 0;
// Add the shot object to the usedGuardiansList, which will be used later to validate a randomization value
// Added objects will not be part of a randomization process
usedGuardians.Add(newPatternList[0]);
}
// This will be the point of entry as soon as the first correct object has been shot
// Get first object of newPatternlist to check against shotObject
// The first time an object is shot, it is always correct, but as this happens, the rest of the object have to be shot in a specific order
GameObject correctGuardian = newPatternList[activationIndex];
// If correct Objectgs has been shot:
if (correctGuardian == shotObject)
{
//Highlight the shotObject
shotObject.GetComponent<Renderer>().material.SetTexture("_EmissiveColorMap", _texture2);
shotObject.GetComponent<Renderer>().material.SetTextureScale("_EmissiveColorMap", new Vector2(1f, 1f));
shotObject.GetComponent<Renderer>().material.EnableKeyword("_EMISSIVE_COLOR_MAP");
shotObject.transform.GetChild(1).gameObject.SetActive(true);
// if activationindex is 12, it means all objects have been shot in the correct order, otherwise:
if(activationIndex < 11)
{
// get the object that should be shot next
nextGuardian = newPatternList[activationIndex+1];
// add it to the usedGuardianlist so that it will not be part of randomization process
usedGuardians.Add(newPatternList[activationIndex+1]);
}
// On default the randomGuardian object is null
// after a correct object has been shot, a new randomGuardian object will be picked from the newPatternlist
// and the old one needs to be deselected (not highlighted anymore)
if(randomGuardian != null)
{
randomGuardian.transform.GetChild(1).gameObject.SetActive(false);
randomGuardian.SetActive(false);
}
// Find a random integer, whill be used to select a randomGuardian object
foundRandomIndex = false;
while(foundRandomIndex == false)
{
randomGuardian = null;
randomIndex = Random.Range(0,newPatternList.Count);
if(!usedGuardians.Contains(newPatternList[randomIndex]))
foundRandomIndex = true;
}
// Get random Object from newPatterList
randomGuardian = newPatternList[randomIndex];
// Add the object that has to be shot next and the randomGuardian object to a list, in order to highlight them
guardsToHighlight.Add(nextGuardian);
guardsToHighlight.Add(randomGuardian);
// Highlight both guardians
foreach (GameObject guardianToHighlight in guardsToHighlight)
{
guardianToHighlight.SetActive(true);
guardianToHighlight.GetComponent<Renderer>().material.SetTexture("_EmissiveColorMap", _texture2);
guardianToHighlight.GetComponent<Renderer>().material.SetTextureScale("_EmissiveColorMap", new Vector2(1f, 1f));
guardianToHighlight.GetComponent<Renderer>().material.EnableKeyword("_EMISSIVE_COLOR_MAP");
guardianToHighlight.transform.GetChild(1).gameObject.SetActive(true);
audioSource = guardianToHighlight.transform.GetChild(0).GetComponent<AudioSource>();
audioSource.Play();
}
if(activationIndex == 11)
{
EventManager.RiddleSolved();
}
// Advance activationIndex to determine which object should be shot next
activationIndex++;
}
}
8901954–1218075–Testing.cs (5.04 KB)