Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding struct (vector3,Vector3) to a list from a random generated number

Discussion in 'Scripting' started by Nachman, Oct 15, 2015.

  1. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Hi everybody,

    I'm learning C# and I don't really like to bother others with questions, I use google and the forums a lot, but I'm having a lot of trouble trying to figure this out and I can't make it work. Basically there is some random value generation which creates an Integer that is used as and indexer in VoxelGrid.voxelCordinates(separate script) which equals to a Vector3, after this I add an other Vector3 to the first and I get two Vector3 values. Up to this point everything works but I need those two Vector3 to get stored in that "temp" variable as a pair of Vector3s as declared in the struct and later on add "temp" to a list of structs, called SegemntsList. Doesn't work, I can't not even print the values of the Vectors3 contained in "temp", I'm going bannanas!!!!
    Here is my code:
     

    Attached Files:

  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    well the struct dosnt define ToString so it likly wont print.
    but can you debug log the 2 fields of the struct individually?
    Code (CSharp):
    1. Debug.Log(temp.A);
    2. Debug.Log(temp.B);
    also you should post your code with code tags instead of attaching it as a file.
    here is the code so others can see it with out downloading.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6.  
    7. public class BusySegments : MonoBehaviour {
    8.     public GameObject segmentPrefab;
    9.     public int spwQuantity;
    10.     public Vector3 origin;
    11.     public Vector3 varH;
    12.    
    13.     public static List<Segment> SegmentsList = new List<Segment>();
    14.    
    15.     public  struct Segment {
    16.         public Vector3 A, B;
    17.        
    18.         public  Segment (Vector3 p1, Vector3 p2) {
    19.             A = p1;
    20.         B = p2;
    21.     }
    22.     }
    23.  
    24.     private void NewNumberH () {
    25.         float spVerticalValue = Random.value;
    26.     int spwQuantity = Mathf.RoundToInt (62 * spVerticalValue + 1);
    27.     Vector3 origin = VoxelGrid.voxelCordinates[spwQuantity];
    28.     Vector3 varH= origin + new Vector3(1,0);
    29.  
    30.         Segment temp = new Segment (origin, varH);
    31.     SegmentsList.Add(temp);
    32.     print (temp);
    33.  
    34.         GameObject vertex = Instantiate(segmentPrefab) as GameObject;
    35.     vertex.transform.localPosition = new Vector3() + origin;
    36.     vertex.transform.localRotation = Quaternion.Euler(0f,90f,90f);
    37.     }
    38. }
    39.  
     
  3. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Hey Thanks a lot passerbycmc!!!!!

    Yeah sorry for the mess, I'm very new to this!