Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting following errors;
No Overload for Method SetTopColor
And "foreach statement cannot operate on variables of type 'Block' because 'Block' does not contain a public instance or extension definition for 'GetEnumerator'"
My code is following;
C#
<pre><pre>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Block))]
public class pathFinder : MonoBehaviour
{
    Block block;
    [SerializeField] Block startPoint, endPoint;
    Dictionary<Vector2Int, Block> pathMap = new Dictionary<Vector2Int, Block>();
    // Start is called before the first frame update
    void Start()
    {
        var block = FindObjectsOfType<Block>();
        LoadBlocks();
    }
    void LoadBlocks()
    {
        
        Vector2Int poS = block.GetGridPos();


        foreach (Block point in block)
        {
            /* Overlapping block should be skipped while
               other should be added to dictionary*/
            if (pathMap.ContainsKey(poS))
            {
                Debug.Log("Overlapping Object: " + point);
            }
            else
            {
                pathMap.Add(poS, block);
            }
        }
        
    }
    void StartEndPointColor()
    {
        startPoint.SetTopColor(Color.black);
    }
}



What I have tried:

I am noob and have bought a course for unity to learn building games but i am not strong enough as yet to troubleshoot this problem on my own.
Posted
Updated 20-May-22 4:31am

1 solution

What you need to do is stop trying to run before you can walk: you need to understand C# rather better than you do at the moment, and certainly understand it pretty well before you start playing with Unity!

The first error is pretty explicit: the method SetTopColor does not take a single argument: Check where you got the information on that from to find out what it should take, and how you should call it.

And a foreach statement basically requires a collection of objects - an array, or a List perhaps - rather than a single object.

As I said, you need to learn C# first, this is pretty basic stuff!
 
Share this answer
 
Comments
NoobZee 20-May-22 11:09am    
Sire You misunderstood!
I am calling SetTopColor from another script called Block.
public void SetTopColor()
{
MeshRenderer topFace = transform.Find("Top").GetComponent<meshrenderer>();
topFace.material.color = Color.black;
}
While in foreach i am calling block variable of Type Block at position poS. Again Block is a collection of objects lying in my Editor Window and Block Script is already attached with multiple objects.

So, your objection about SetTopColor and Collection of Objects in foreach loop is due to limited information i provided in the thread, Maybe i should have provided all scripts here.
OriginalGriff 20-May-22 11:45am    
So ... yout code that works passes no parameters, and your method doesn't want any.
And you are surprised when you get an error because you are passing a parameter to it?

And if Block doesn't implement IEnumerable then it can't be called a collection, and won't work with foreach.

Seriously, you need to know this stuff ...
NoobZee 20-May-22 11:57am    
Thank you for your kind suggestion.
OriginalGriff 20-May-22 12:35pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900