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
0
Question by edgartip2003 · Aug 23, 2020 at 05:16 PM · instantiateunity 2dpublic variable

My variables arent updating accordingly after I instantiated them

I am trying to instantiate the rock blocks in a grid style and save their information in another script via public ints. The blocks spawn perfectly as intended but the information doesn't save correctly. I set the default values on the prefabs of the gameObjects that I create to 0 and isDead to false. The first 3 gameobject "clones" spawn with all the information to 0 and after the it saves 1 or 2 correctly (might be luck) but then it becomes pretty much random. Any help would be greatly appreciated!

The script that saves the positions :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //Save the spawn position in the array of all the tiles
 public class TileInformation : MonoBehaviour
 {
     public int PositionInLayer;
     public int xPositionInLayer;
     public int yPositionInLayer;
     public int layerDept;
     public bool isDead = false;
 
 
 }
 

The script that creates the prefabs (Only pasting the part that isn't working as intended)

     //Values to make the grid
     public int amountOfLayers;
     public int sideSize;
     public int heightSize;
     private int layerCounter = 0;
 
 
     //The tiles to spawn
     public GameObject SandStoneTile;
     public GameObject LightRockTile;
     public GameObject RockTile;
     public GameObject SandFloor;
 
     //Gets us the initial posiiton
     public Transform initialPosition;
 
     //Get all the selected tiles
     GameObject[,] allTiles;
 
 
     private void Start()
     {
 
         //while we haven't ocupied all the layers
         int blocksSide;
         int blocksHeight;
         int arrayCounter;

         //Fill all the layers with blocks
         while (layerCounter != amountOfLayers)
         {
             blocksSide = 0;
             blocksHeight = 0;
             arrayCounter = 0;
             
             //while we havent ocupied all block in a certain layer
             
          
             while (arrayCounter != heightSize * sideSize)
             {
 
                 //Get random gameObjectTile
                 GameObject SelectedObject = RandomPicker();

                 //Instantiate our random Object
                 Instantiate(SelectedObject, new Vector3(initialPosition.position.x + blocksSide, initialPosition.position.y + blocksHeight * -1, 1 * layerCounter),Quaternion.identity);
 
                 //Put the newly created block into an array. 
                 allTiles[layerCounter ,arrayCounter] = SelectedObject;

                //I used this to see if the numbers are set correctly which they are!
                 Debug.Log("Counter:" + arrayCounter + "GameObject:" + SelectedObject.name + "Positionx:" + blocksSide + "Position y :" + blocksHeight);
 
                 //This will save where in, another script, a determained clone gameobject is 
                 TileInformation TilePosition = SelectedObject.GetComponent<TileInformation>();
                 TilePosition.layerDept = layerCounter;
                 TilePosition.xPositionInLayer = blocksSide;
                 TilePosition.yPositionInLayer = blocksHeight;
                 TilePosition.PositionInLayer = arrayCounter;
 
 
                 //Increment to the next position
                 arrayCounter++;
                 blocksSide++;
 
                 //if we get to the last position in the grid, we go down if it isnt the last position
                 if (blocksSide == sideSize && arrayCounter != heightSize * sideSize)
                 {
                     blocksSide = 0;
                     blocksHeight++;
                 }
                
             }
             layerCounter++;
         }
 }
 
 //To pick a random Gameobject Note : Yes I know it would be better with a switch case :P
   GameObject RandomPicker()
     {
         float value = Random.Range(0f, 1f);
 
         //60% chance of a Sand Tile, 30% chance of a lightRock Tile and 10% of a Rock Tile
         if (Dificulty == "Easy")
         {
             if (value <= 0.6f) return SandStoneTile;
             else if (value > 0.6f && value <= 0.9f) return LightRockTile;
             else return RockTile;
         }
 
         //45% chance of a Sand Tile, 30% chance of a lightRock Tile and 25% of a Rock Tile
         else if (Dificulty == "Medium")
         {
             if (value <= 0.45f) return SandStoneTile;
             else if (value > 0.45f && value <= 0.75f) return LightRockTile;
             else return RockTile;
         }
 
         //30% chance of a Sand Tile, 30% chance of a lightRock Tile and 40% of a Rock Tile
         else if (Dificulty == "Hard")
         {
             if (value <= 0.3f) return SandStoneTile;
             else if (value > 0.3f && value <= 0.6f) return LightRockTile;
             else return RockTile;
         }
         //if something goes wrong 
         return SandFloor;
     }

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 N-8-D-e-v · Aug 23, 2020 at 07:27 PM 0
Share

Have you hit apply on your prefab?

avatar image edgartip2003 N-8-D-e-v · Aug 23, 2020 at 08:24 PM 0
Share

Yes I have!

1 Reply

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

Answer by highpockets · Aug 23, 2020 at 09:25 PM

Your SelectedObject is not a reference to a new GameObject, it is a reference to the prefab. So you are creating the prefab and then you are just “saving” the values on the Prefab, not the instantiated object. You need to get a reference to the instantiated object instead:


 GameObject SelectedObject =  Instantiate(RandomPicker(), new Vector3(initialPosition.position.x + blocksSide, initialPosition.position.y + blocksHeight * -1, 1 * layerCounter),Quaternion.identity);





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 edgartip2003 · Aug 23, 2020 at 09:40 PM 0
Share

Thank you so much man! $$anonymous$$ade my day

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

138 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

Related Questions

Need to figure out how to Instantiate going down on the y axis. 0 Answers

Can't instantiate "scene objects" [Unity Mirror Multiplayer] 0 Answers

Adjacent Placement of Objects according to varying height 0 Answers

How can I instantiate a different sprite depending on where Raycasting goes? 0 Answers

Only getting one game object instead of multiple game objects in start of the program 0 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