- Home /
I need to alter all instances of a prefab?
I am working on implementing a cheatcode into my game, which is a modified version of the Space Shooter tutorial. It's working a little, but there are still some kinks.
using UnityEngine;
using System.Collections;
public class CheatCodeListener : MonoBehaviour
{
private string[] cheatCode;
private int index;
private GameObject asteroid;
private DestroyByContact destroyByContact;
void Start()
{
cheatCode = new string[] { "c"};
index = 0;
}
void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKeyDown(cheatCode[index]))
{
index++;
}
else
{
index = 0;
}
}
if (index == cheatCode.Length)
{
index = 0;
CheatCode();
}
}
void CheatCode ()
{
asteroid = GameObject.FindGameObjectWithTag("Asteroid");
destroyByContact = asteroid.GetComponent<DestroyByContact>();
print("Cheatcode Entered!");
destroyByContact.scoreValue = 100;
}
The big one is that when I press C (the cheat code) it only changes scorevalue
to 100 for the first asteroid, but none of the others. They are all worth the 10 points that is the default value. The asteroids spawn 10 at a time, with a few seconds between waves. All asteroids are a copy of a prefab asteroid, which is tagged Asteroid. For this question, I need to know how to change the value for all asteroids that will spawn from the time the code is entered until the game ends.
Answer by psyydack · Jun 11, 2014 at 03:24 AM
Full code tested.
using UnityEngine;
using System.Collections;
public class CheatCodeListener : MonoBehaviour
{
private string[] cheatCode;
private int index;
//remove this line you dont need //private GameObject asteroid;
private DestroyByContact destroyByContact;
void Start ()
{
cheatCode = new string[] { "c" };
index = 0;
}
void Update ()
{
if (Input.anyKeyDown) {
if (Input.GetKeyDown (cheatCode [index])) {
index++;
} else {
index = 0;
}
}
if (index == cheatCode.Length) {
index = 0;
CheatCode ();
}
}
void CheatCode ()
{
GameObject[] asteroids = GameObject.FindGameObjectsWithTag ("Asteroid");
foreach (GameObject asteroid in asteroids) {
destroyByContact = asteroid.GetComponent<DestroyByContact> ();
destroyByContact.scoreValue = 100;
}
}
}
using UnityEngine;
using System.Collections;
public class CheatCodeListener : $$anonymous$$onoBehaviour
{
private string[] cheatCode;
private int index;
private GameObject asteroid;
private DestroyByContact destroyByContact;
void Start()
{
cheatCode = new string[] { "c"};
index = 0;
}
void Update()
{
if (Input.any$$anonymous$$eyDown)
{
if (Input.Get$$anonymous$$eyDown(cheatCode[index]))
{
index++;
}
else
{
index = 0;
}
}
if (index == cheatCode.Length)
{
index = 0;
CheatCode();
}
}
void
{
Object asteroids = GameObject.FindGameObjectsWithTag("Asteroid");
foreach (Object asteroids in asteroid)
{
destroyByContact = asteroid.GetComponent<DestroyByContact>();
destroyByContact.scoreValue = 100;
}
}
}
Error: Assets/Scripts/CheatCodeListener.cs(45,33): error CS0136: A local variable named asteroids' cannot be declared in this scope because it would give a different meaning to
asteroids', which is already used in a `parent' scope to denote something else
I get the feeling that this is a simple error to fix, just rename something, but I don't know what. I don't quite understand the error.
Sorry about that, just change this line : foreach (Object asteroid in asteroids)
Not a problem, I appreciate the help. Still not working though
using UnityEngine;
using System.Collections;
public class CheatCodeListener : $$anonymous$$onoBehaviour
{
private string[] cheatCode;
private int index;
private GameObject asteroid;
private DestroyByContact destroyByContact;
void Start()
{
cheatCode = new string[] { "c"};
index = 0;
}
void Update()
{
if (Input.any$$anonymous$$eyDown)
{
if (Input.Get$$anonymous$$eyDown(cheatCode[index]))
{
index++;
}
else
{
index = 0;
}
}
if (index == cheatCode.Length)
{
index = 0;
CheatCode();
}
}
void CheatCode()
{
Object asteroids = GameObject.FindGameObjectsWithTag("Asteroid");
foreach (Object asteroid in asteroids)
{
destroyByContact = asteroid.GetComponent<DestroyByContact>();
destroyByContact.scoreValue = 100;
}
}
}
Two errors. The first
Assets/Scripts/CheatCodeListener.cs(43,24): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to
UnityEngine.Object'
The second
Assets/Scripts/CheatCodeListener.cs(45,17): error CS1579: foreach statement cannot operate on variables of type UnityEngine.Object' because it does not contain a definition for
GetEnumerator' or is not accessible
Check the answer again, I update it. Hope it works for your game. =)
Your updated code got rid of the errors, but still didn't do what I wanted. It changed the scorevalue to 100 for all asteroids that were in the scene at the time of the cheatcode entry. I made a few $$anonymous$$or changes. I added a bool variable called cheatActive and set it to false in Start ()(so that when the player died and restarted the game, the cheatcode wouldn't carry over). I then put it so that when the cheatcode was entered, cheatActive was set to true. I created an If statement, and put the code from the CheatCode () function inside of it, like so
if(cheatActive == true)
{
GameObject[] asteroids = GameObject.FindGameObjectsWithTag ("Asteroid");
foreach (GameObject asteroid in asteroids) {
destroyByContact = asteroid.GetComponent<DestroyByContact> ();
destroyByContact.scoreValue = 100;
}
And that's all it took. The cheat works for all asteroids that are spawned after the cheatcode is entered. $$anonymous$$uch to my surprise, this code works even between waves, when there are no objects in the scene tagged Asteroid. I figured entering it between waves like that would cause a null reference error. I don't know why this happens, but hey, that's what I wanted anyway, so I can't complain. If you can explain why that happens, I'd be grateful. If not, don't worry about it :) Thanks a million for all of your help @psyydack
Your answer
Follow this Question
Related Questions
How to make asteroids bounce off eachother 0 Answers
Modified Roll a Ball Tutorial,Modified Roll a Ball tutorial question 1 Answer
UnityException: Tag: Gamecontroller is not defined.@Space shooter tutorial.. 4 Answers
How do I re-link an object that has unintentionally become detached from prefab? 0 Answers