- Home /
How can i sum the number of generated nodes each time clicking the checkbox ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NodesGenerator : MonoBehaviour {
public GameObject NodePrefab;
public int NodesNumberXY = 16;
public bool Generate = false;
public int CreatedNodes;
private void Start()
{
if (NodesNumberXY % 2 == 1)
{
NodesNumberXY = 0;
}
else
{
Generating();
}
}
private void Update()
{
if (Generate == true)
{
if (NodesNumberXY % 2 == 1)
{
NodesNumberXY = 0;
}
else
{
Generating();
}
Generate = false;
}
}
void Generating()
{
for (int x = 0; x < NodesNumberXY; x++)
{
for (int y = 0; y < NodesNumberXY; y++)
{
GameObject cube = Instantiate(NodePrefab);
cube.tag = "Node";
cube.transform.localScale = new Vector3(4, 1, 4);
cube.transform.position = new Vector3(x * 5, 0, y * 5);
}
}
CreatedNodes = NodesNumberXY * NodesNumberXY;
}
}
In the variable CreatedNodes i set the number of created nodes. But when the game is running and i check the checkbox more and more times it's creating more nodes but the variable CreatedNodes not change. For example if the value in NodesNumberXY is 16 then it will create 256 nodes.
Now if i check the checkbox again it's creating more 256 nodes but the value in NodesNumberXY is still 256 and it should be 512.
Answer by Bunny83 · Apr 10, 2017 at 10:18 PM
This line:
CreatedNodes = NodesNumberXY * NodesNumberXY;
does not add the generated node amount to your variable but set the variable to that amount. You most likely want to do:
CreatedNodes += NodesNumberXY * NodesNumberXY;
Your answer
Follow this Question
Related Questions
How can i lock the mouse cursor in the middle of the screen ? 1 Answer
Why InputField don't have the property text ? 2 Answers
How can i change the walls height and keep the size for example 100x100 but height 0.1 ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers