Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 matthew_gigante · Dec 08, 2017 at 05:19 PM · c#instantiateprefabarrayarrays

Prefabs instantiated from an array are keeping their public int value

I have a script which produces 6 blocks side by side, from a possible array of 3 different blocks. The array consists of the items in my prefabs folder. When my code runs and the game starts, I need each of the blocks spawned to have a numberOfHits value that equals zero, but the prefabs in the folder are holding onto values added to them, and each item instantiated from the array holds the same number. I have tried using the start function of the prefabs but nothing is working, I think there is an issue with how I am instantiating, so here is the code for that: EDIT: I suppose what I am also asking for is how to add +1 to an integer with the specific object I spawned, so that If I add one to a clone of a dirt block, it does not add 1 to every dirtblock in the scene or the prefab, just that specific block that the player is colliding with via raycast.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //the floor spawner will spawn a random set of different types of terrain
 //the terrain will spawn in one of 6 equal length spots, next to each other
 //if the player's y value changes, the next set of 6 terrain blocks will spawn 
 //in a set amount of space under the last set of 6 blocks
 public class floorSpawner : MonoBehaviour {
 
     //here we set arrays for the floors and the positions possible for them to spawn in
     public GameObject[] floors;
     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
     //these are the specific coordinates that a block can be spawned on
     public int spawnPointSpots;
 
     //handles the spawnZone spawning, when the player passes through these the next set of floors spawn
     public GameObject spawnZone;
     public Transform spawnZoneTrans;
 
     // Use this for initialization
     public void Start () {
         spawnPointSpots = 0;
         Spawn();
     }
 
     //spawns a random block in each slot in the row, runs in a loop till all spots are filled
     void Spawn(){
         Instantiate (spawnZone, new Vector3 (spawnZoneTrans.position.x, spawnZoneTrans.position.y - 4), spawnZoneTrans.rotation);
         for (int i = 0; i < 6; i++) {
             int floorsIndex = Random.Range (0, 3);
             int spawnPointIndex = spawnPointSpots;
             // Create an instance of the floor prefab at the randomly selected spawn point's position and rotation.
             Instantiate (floors [floorsIndex], new Vector3 (spawnPoints [spawnPointIndex].position.x, spawnPoints [spawnPointIndex].position.y - 4), spawnPoints [spawnPointIndex].rotation);
             spawnPointSpots++;
             }
     }
 }
 
Comment
Add comment · Show 2
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 Dray · Dec 08, 2017 at 06:38 PM 0
Share

GameObject.Instantiate does instantiate a copy of the specified GameObject so this part of the code seems valid to me (though it sounds like there is an active prefab connection in the first place). Can you add some information on how/when/where your are storing and incrementing the numberOfHits variable?

avatar image Minuks · Dec 09, 2017 at 03:03 AM 0
Share

Could you post the code where the variable numberOfHits is used ?

1 Reply

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

Answer by matthew_gigante · Dec 14, 2017 at 01:57 PM

Making changes to small parts of the 3 scripts related worked for me, the scripts are here:

 //Matthew Gigante 2017
 //A helpful tutorial related to this code:
 //https://unity3d.com/learn/tutorials/projects/2d-ufo-tutorial/controlling-player
 
 //the current objective of this script is to move the player
 //and to prevent them from falling off the sides of the screen.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class playerController : MonoBehaviour {
 
     private int numberofhitsinscript;
     //this public variable will contain the player GameObject
     public GameObject player;
     //this will be the speed of our player, which can be changed in the Unity inspector
     public float speed;
     //allows access to the floorSpawner script, which will help us instantiate new floor prefabs
     public floorSpawner other;
     public blockScript scriptForBlocks;
 
     void Update ()
     {
         //this if/if else statement here will check to see if the player would go off the right or left of the screen
         //if the player is about to go off the screen, instead put them back where in the x position they were at before that
         if (player.transform.position.x <= -2.6f) {
             player.transform.position = new Vector3 (-2.6f, transform.position.y, 0);
         } else if (transform.position.x >= 2.6f) {
             player.transform.position = new Vector3 (2.6f, transform.position.y, 0);
         }
         //These two if statements wait for input, and will move the player left or right if the arrows are pressed
         //this is placeholder code, eventually this will be replaced with onMouseDown and buttons so that it may be optimized for android use
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         if(Input.GetKeyDown(KeyCode.DownArrow))
         {
             //middle vector2 is size (Vector2.one)
             //creates a collider for the ground, detects overlap between player and whatever ground object is below it
             //if there is an object directly under the player object, destroy it
             Collider2D ground = Physics2D.OverlapBox (transform.position + Vector3.up * -0.6f, Vector2.one * .01f, 0f);
             print (ground.gameObject);
 
             //add " || bedRock(Clone) " 
             //if(ground.gameObject.name = "bedRock(Clone){
             // play a dink noise and do not destroy bedrock
 
             if(ground.gameObject.name != "spawnZone(Clone)"){
                 //scriptForBlocks.addScript ();
                 //scriptForBlocks.mineHandler ();
                 //Destroy (ground.gameObject);
                 ground.gameObject.GetComponent<blockScript>().addScript ();
                 ground.gameObject.GetComponent<blockScript>().mineHandler ();
                 }
         }
     }
     //runs floorSpawner script's Start function
     void OnTriggerEnter2D(Collider2D coll){
         other.Start ();
     }
 }

 //Matthew Gigante 2017
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //the floor spawner will spawn a random set of different types of terrain
 //the terrain will spawn in one of 6 equal length spots, next to each other
 //if the player's y value changes, the next set of 6 terrain blocks will spawn 
 //in a set amount of space under the last set of 6 blocks
 public class floorSpawner : MonoBehaviour {
 
     //here we set arrays for the floors and the positions possible for them to spawn in
     public GameObject[] floors;
     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
     //these are the specific coordinates that a block can be spawned on
     public int spawnPointSpots;
 
     //handles the spawnZone spawning, when the player passes through these the next set of floors spawn
     public GameObject spawnZone;
     public Transform spawnZoneTrans;
 
     // Use this for initialization
     public void Start () {
         spawnPointSpots = 0;
         Spawn();
     }
 
     //spawns a random block in each slot in the row, runs in a loop till all spots are filled
     void Spawn(){
         Instantiate (spawnZone, new Vector3 (spawnZoneTrans.position.x, spawnZoneTrans.position.y - 4), spawnZoneTrans.rotation);
         for (int i = 0; i < 6; i++) {
             int floorsIndex = Random.Range (0, 4);
             int spawnPointIndex = spawnPointSpots;
             // Create an instance of the floor prefab at the randomly selected spawn point's position and rotation.
             Instantiate (this.floors [floorsIndex], new Vector3 (spawnPoints [spawnPointIndex].position.x, spawnPoints [spawnPointIndex].position.y - 4), spawnPoints [spawnPointIndex].rotation);
             spawnPointSpots++;
             }
     }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class blockScript : MonoBehaviour {
 
     public int numberOfHits;
     public int maxHits;
     public string currentBlock;
     //public GameObject block;
 
     public void start(){
         numberOfHits = 0;
     }
 
     public void addScript(){
         this.numberOfHits++;
     }
 
     public void mineHandler(){
         Debug.Log(numberOfHits);
             if(numberOfHits == maxHits){
             Destroy (this.gameObject);
         
         }
     }
 }
 



Comment
Add comment · Show 1 · 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 megabrobro · Dec 14, 2017 at 02:10 PM 0
Share

good job posting how you fixed it. Would be even nicer if you could do a short summary sentence explaining what was missing/wrong

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

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

Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

How to deal with for loop and array? 0 Answers

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

OverlapSphere for parallel arrays 1 Answer


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