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 NGKrush1 · Apr 19, 2013 at 07:28 PM · c#gameobjectraycastlistcompare

compare raycast hit with list of gameobjects c#

Hi,

I'm working on a simple game which is made out of seperate cubes on a grid. if i clicking a cube (saved, elsewhere in the code, as RayCastHit hitOG), it adds the cube left/right/above/below to a list (so 4 cubes).

now, I'm trying to get it that after clicking the cube, i want an action to occur only when clicking one of these cubes in the list.

this is my (simplified) code:

 if (Input.GetMouseButtonDown(0))
 {
 ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
 if(Physics.Raycast(ray,out hit,100))
 {
 //when clicking the same cube, this part works.
 if (hit.transform.name == hitOG.transform.name)
 {
 //reset the game
 }
 //now I check "public List<GameObject> GOs" which is in another script.
 foreach (GameObject gObj in makeSphere.GOs) 
 {
 if (gObj.transform.name == hit.transform.name)
 {
 //do something important
 }
 }
 }
 }

I cannot get the foreach to work. it is a problem comparing raycasthit with gameobjects? I've looking at this all day, but cannot find a solution. I hope someone can help.

Many thanks!!

Comment
Add comment · Show 5
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 kmeboe · Apr 19, 2013 at 08:56 PM 0
Share

Nothing jumps out at me. To start debugging, I would add Debug.Log() calls where you print out the name of the hit object, and then the names of each object in the list.

avatar image whydoidoit · Apr 19, 2013 at 10:41 PM 0
Share

Are your gObj actually prefabs? Or the real objects.

avatar image NGKrush1 · Apr 22, 2013 at 01:16 PM 0
Share

thanks for the replies. the gObj are prefabs yes. I've tried to Debug.Log(makeSphere.GOs.Count) and write it out in OnGUI() but it always returns 0 even though the elements are given in the inspector of unity when i pause the programme.

Still stuck on this one.. :(

also tried: Debug.Log(makeSphere.GOs[0].transform.name);

but got this error:

 ArgumentOutOfRangeException: Argument is out of range.
 Parameter name: index
 System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Collections.Generic/List.cs:633)
 $$anonymous$$ain.Update () (at Assets/Scripts/$$anonymous$$ain.cs:97)

update: it has something to do with the makeSphere.GOs because when i write Debug.Log(makeSphere.GOs.Count) in makeSphere (so Debug.Log(GOs.Count)) it gives the correct amount. somehow my original file wont read out the List I created in the other file.

-this doesn't solve the problem as i really need it in the first file.. :/

avatar image kmeboe · Apr 22, 2013 at 07:53 PM 0
Share

Do you have an instance of makeSphere in the other file, or are you trying to access the class statically? In other words, are you sure that the "makeSphere" in one file, and the makeSphere that you're looking at in the makeSphere file are the same?

avatar image NGKrush1 · Apr 23, 2013 at 11:21 AM 0
Share

I don't think I do. I'm afraid I don't know how to do that within unity, could you point me in the right direction. $$anonymous$$any thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Apr 22, 2013 at 05:29 PM

The name of an instantiated prefab has "(Clone)" appended to it - so your name test won't work - you could do:

  if(hit.transform.name.StartsWith(gObj.name))
  {
  }
Comment
Add comment · 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
0

Answer by kmeboe · Apr 23, 2013 at 11:25 PM

Judging by your comments above, it sounds like you might not be accessing the correct makeSphere object.

There are a couple of ways you can make sure that you can fix this. Probably the easiest way would be to add a public member to the class where you want to access makeSphere. Here's some quick code:

 //makeSphere is of type "Foo" in this example.
 
 public class classWhereMakeSphereLives : MonoBehaviour
 {
     // You could also replace these two lines with a property, as desired.
     Foo makeSphere;
     Foo GetMakeSphere(){ return makeSphere; }
 }
 
 public class classWhereYouWantToAccessMakeSphere : MonoBehaviour
 {
     // classWhereMakeSphereLives doesn't actually live here.  Instead, you
     // will use the inspector to drag the object that has classWhereMakeSphereLives
     // into the following public field:
     public classWhereMakeSphereLives makeSphereOwner;
 
     // You can now access makeSphere here, like so:
     void Update
     {
         Foo makeSphereReference = makeSphereOwner.GetMakeSphere();
         makeSphereReference.GOs[]......
     }
 
 }

Why are the class names so long? That's self-documenting code, baby! :D

Comment
Add comment · Show 9 · 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 NGKrush1 · Apr 24, 2013 at 11:38 AM 0
Share

thanks for all your effort (the long classnames really help :D), this sounds like the correct direction to solve the problem. After implementing this code I get the error:

NullReferenceException: Object reference not set to an instance of an object

I had to make

-Foo Get$$anonymous$$akeSphere(){ return makeSphere; }

public to access it in the other class.

avatar image kmeboe · Apr 24, 2013 at 06:12 PM 0
Share

Good catch on the "public" part.

I'm guessing the problem is that you need to create the makeSphere object. So for example, you could do this:

 Foo makeSphere = new Foo();

That would go in place of the current definition. Or you could do it inside Start, like this:

 void Start()
 {
     // Note: no leading "Foo" here, since it's already declared as a field in the class.
     makeSphere = new Foo();
   
     // Now you can populate makeSphere or whatever.
 }
avatar image NGKrush1 · Apr 25, 2013 at 10:55 AM 0
Share

I'm very sorry, but I can't get it working. I created the line you ask in both files (didn't understand which one) but for both it didn't solve the problem.

$$anonymous$$y c# skills are probably not far enough to comprehend this problem. I'll report my code here again, maybe I'm just asking the wrong questions..

So this is the first class called "$$anonymous$$ain":

 void Update () 
     {            
         //click mouse
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             Debug.Log ("GOs count: " + makeSphere.GOs.Count); //this results in 0 at all times.
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                         
             if(Physics.Raycast(ray,out hit,100))
             {    
                 //this piece happens after the codeblock under here.                            
                 if (playerActive == true && hit.transform.name != "dice")
                 {
                     if (hit.transform.name == hitOG.transform.name) //if ray hit is the same as prev click
                     {
                         Debug.Log("I clicked the same activesquare");
                         playerActive = false;                        
                         //destroy all created stuff here
                     }
                                         
                     foreach (GameObject gObj in makeSphere.GOs) //this part doesn't work
                     {                    
                         if (gObj.transform.name == hit.transform.name) { /*succes!!*/ }
                     }                                                                                                                            
                 }
                 
                 //so this piece of code happens first
                 if (playerActive == false && hit.transform.name != "dice" && stillActive == false)
                 {    
                     Debug.Log("I started player active. B succesful");
                     hitOG = hit;
                     Debug.Log("Created hitOG = " + hitOG.transform.name);
                     playerActive = true;
                     Instantiate(sphereSelect, hit.transform.position, hit.transform.rotation); //here i create a prefab with the other class "maakSphere"
                 }                                                        
             } //end if raycast        
         } //end if input    
     } //end update

Then this is the code from the prefab, called "maakSphere":

 public class maakSphere : $$anonymous$$onoBehaviour {
     
     //list of all collided GOs 
     public List<GameObject> GOs = new List<GameObject>();
     //find sphere and move/kill
     public List<GameObject> killThem$$anonymous$$aak = new List<GameObject>();            
     // blackcube as selection outline
     public GameObject blackSelect;
     
     //this is your code
     maakSphere makeSphere;
     public maakSphere Get$$anonymous$$akeSphere(){ return makeSphere; }    
      
     void OnTriggerEnter (Collider Other)
     {
         GOs.Add(Other.gameObject);
         // i know i'm instantiating objects on the same position atm...
         foreach (GameObject gob in GOs)
         {
             Vector3 blackObject = gob.transform.position;
             blackObject.z = blackObject.z + 2;
             Instantiate(blackSelect, blackObject, gob.transform.rotation);
         }            
 
         //and that this is a mess...
         GameObject[] killThem$$anonymous$$S = new GameObject[10];
         killThem$$anonymous$$S = GameObject.FindGameObjectsWithTag("selSphere");
         foreach (GameObject gobble in killThem$$anonymous$$S)
         {
             killThem$$anonymous$$aak.Add(gobble);
         }
         foreach (GameObject gObj in killThem$$anonymous$$aak) 
         {
             //Destroy(gObj);
             //test
             gObj.transform.Translate(0,0,10);
         }
         killThem$$anonymous$$aak.Clear();    
     }
avatar image kmeboe · Apr 25, 2013 at 04:58 PM 0
Share

You can remove my code from maakSphere; this isn't the right spot for it.

We'll try to figure out what to do next. Did you drag your maakSphere class onto an object in your scene? What's the name of that object?

avatar image NGKrush1 · Apr 25, 2013 at 05:15 PM 0
Share

thanks so much for your help.

maakSphere is connected to a prefab called selectSphere, which is instantiated on line #34 of the $$anonymous$$ain class. This is a spherecollider that (in maakSphere) looks for which objects it collides with and adds it to List GOs (line #16 in maakSphere)

Show more comments

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

13 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

Related Questions

AI Instantiate targets/prefab. (pick target issue) 1 Answer

Get first gameobject in a list and cycle through on keypress 1 Answer

Ordering a list of GameObjects 3 Answers

Getting game logic object from GameObject 1 Answer

Add Object to List of GameObjects C# 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