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
0
Question by Jess23 · Jan 27, 2017 at 12:58 AM · classforeach

getting values from class

hello i'm trying to retrieve variables from a class without success the foreach loop is ignored it seems any ideas?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class myscript : MonoBehaviour
 {
     class myclass
     {
         public string t;
         public int r;
         public int m;
 
         public myclass(string newT, int newR, int newM)
         {
             t = newT;
             r = newR;
             m = newM;
         }
     }
     
     List<myclass> mylist = new List<myclass>();
 
         void Start()
     {
         mylist.Add(new myclass("0", 0, 0));
         mylist.Add(new myclass("2", 0, 0));
         mylist.Add(new myclass("3", 0, 0));
 
         foreach (myclass m in mylist)
         {
             print("---check---");
             print(m.t + " - " + m.r + " - " + m.m);
         }
     }
 }

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 Bunny83 · Jan 27, 2017 at 03:04 AM 0
Share

I don't see any problem here. You should be more specific about what you have tried and what actually happens. You haven't said anything about your setup. So are you sure that:

  • Your script is attached to a gameobject in the scene?

  • the gameobject where the script is attached to is active (not deactivated)

  • every parent of the gameobject is also active. The active state is inherited down the hierarchy

  • your script component is not disabled.

All those points can be verified at once by placing a Debug.Log / print at the very first line inside your Start method to see if Start is executed at all. If it does execute you should see your prints in the console.

Again you should add more details about your issue. What does the console show? Anything? $$anonymous$$aybe an error? If there are compiler errors, even in another script, your script won't work since all scripts are compiled into one assembly.

avatar image EDevJogos · Jan 27, 2017 at 03:39 AM 0
Share

alt text

I simple copy past your code into unity and it works, as bunny asked, is your script attached to a gameobject in the scene?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by juicyz · Jan 27, 2017 at 01:15 AM

Try 'Debug.Log' instead of 'print'

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 Bunny83 · Jan 27, 2017 at 03:06 AM 0
Share

That won't change anything. "print" is a static method of the $$anonymous$$onoBehaviour class and is literally defined as:

 public static void print(object message)
 {
     Debug.Log(message);
 }

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Foreach - what am I doing wrong? 1 Answer

Can't use method from custom class during foreach 2 Answers

Problem with button press 2 Answers

Overriding a function. 1 Answer

Null reference when trying to return an object 2 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