Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 caleb_b · Jun 11, 2014 at 02:56 AM · prefabspace shooterpoints

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
         }
     }
  }
Comment
Add comment · Show 6 · 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 caleb_b · Jun 11, 2014 at 03:51 AM 0
Share
 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.

avatar image psyydack · Jun 11, 2014 at 01:44 PM 0
Share

Sorry about that, just change this line : foreach (Object asteroid in asteroids)

avatar image caleb_b · Jun 11, 2014 at 03:42 PM 0
Share

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

avatar image psyydack · Jun 12, 2014 at 06:54 AM 1
Share

Check the answer again, I update it. Hope it works for your game. =)

avatar image caleb_b · Jun 12, 2014 at 08:09 PM 0
Share

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

Show more comments

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

22 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

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

Objects flickering between dark and light 7 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