My code does not work

I figured out the problem

I am making a level for my game world building class with Unity C# and part of the code will not work. I keep getting red underlines under the second set of Jellyfish I did not write the original it was provided to me and I was editing it for my level. The errors I am getting are CS0118, CS0119, and CS0131.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter)),RequireComponent(typeof(MeshRenderer))]
[ExecuteInEditMode]
public class Jellyfish:MonoBehaviour{

	[HideInInspector]
	public  MeshRenderer mr;
	private Material mat;
	private MeshFilter mf;
	private Mesh mesh;
	private List<Vector3> vertices=new List<Vector3>(200);
	private List<Vector3> uvs=new List<Vector3>(200);
	private List<Color> colors=new List<Color>(200);
	private int[] triangles;

	public Texture2D texture;
	private Texture2D _texture;

	public Color tint=Color.white;
	private Color _tint;

	[Range(0,30)]
	public int divisionsX=10;
	private int _divisionsX;

	[Range(0,30)]
	public int divisionsY=10;
	private int _divisionsY;

	public enum JellyDirections{Vertical,Horizontal};
	public JellyDirections JellyfishDirection= JellyDirections.Vertical;
	private JellyDirections _JellyfishDirection;

	public enum objSides{Top,Right,Bottom,Left,None}
	public objSides staticSide=objSides.Bottom;
	private objSides _staticSide;

	[Range(-10,10)]
	public float JellyfishFrequency=10f;
	private float _JellyfishFrequency;

	[Range(0f,1f)]
	public float JellyfishForce=0.03f;
	private float _JellyfishForce;

	[Range(0f,10f)]
	public float JellyfishSpeed=1f;
	private float _JellyfishSpeed;

	float meshWidth=1;
	float meshHeight=1;

	[HideInInspector]
	public int sortingLayer=0;
	private int _sortingLayer;

	[HideInInspector]
	public int orderInLayer=0;
	private int _orderInLayer=0;

	void OnEnable(){
		mr=GetComponent<MeshRenderer>();
		mf=GetComponent<MeshFilter>();
		SetMeshAndMaterial();
		GenerateMesh();
		sortingLayer=mr.sortingLayerID;
		orderInLayer=mr.sortingOrder;
        Update();
    }

	void Update(){
		if(
			_texture!=texture || 
			_tint!=tint ||
			_divisionsX!=divisionsX || 
			_divisionsY!=divisionsY || 
			_JellyfishDirection!= JellyfishDirection || 
			_staticSide!=staticSide || 
			_JellyfishFrequency!=JellyfishFrequency || 
			_JellyfishForce!=JellyfishForce ||
			_JellyfishSpeed!= JellyfishSpeed ||
			_sortingLayer!=sortingLayer ||
			_orderInLayer!=orderInLayer
		){
			SetMeshAndMaterial();
			_divisionsX=divisionsX;
			_divisionsY=divisionsY;
			if(_JellyfishDirection!=JellyfishDirection){
				mat.SetFloat("_Jellyfish",JellyfishDirection==JellyDirections.Vertical?0:1);
				_JellyfishDirection=JellyfishDirection;
			}
			if(_staticSide!=staticSide){
				if(staticSide==objSides.Top) mat.SetFloat("_StaticSide",1);
				if(staticSide==objSides.Right) mat.SetFloat("_StaticSide",2);
				if(staticSide==objSides.Bottom) mat.SetFloat("_StaticSide",3);
				if(staticSide==objSides.Left) mat.SetFloat("_StaticSide",4);
				if(staticSide==objSides.None) mat.SetFloat("_StaticSide",0);
				_staticSide=staticSide;
			}
			_JellyfishFrequency=JellyfishFrequency;
			_JellyfishForce=JellyfishForce;
			_JellyfishSpeed=JellyfishSpeed;
			mat.SetFloat("_JellyfishFrequency",JellyfishFrequency);
			mat.SetFloat("_JellyfishForce",JellyfishForce);
			mat.SetFloat("_JellyfishSpeed",JellyfishSpeed);
			if(_texture!=texture){
				_texture=texture;
				mat.SetTexture("_MainTex",texture);
				if(texture!=null){
					if(texture.width>texture.height){
						meshWidth=1f;
						meshHeight=(float)texture.height/(float)texture.width;
					}else{
						meshWidth=(float)texture.width/(float)texture.height;
						meshHeight=1f;
					}
				}else{
					meshWidth=1f;
					meshHeight=1f;
				}
			}

			_tint=tint;
			mat.SetColor("_Color",tint);

			if(_sortingLayer!=sortingLayer || _orderInLayer!=orderInLayer){
				mr.sortingLayerID=sortingLayer;
				mr.sortingOrder=orderInLayer;
				_sortingLayer=sortingLayer;
				_orderInLayer=orderInLayer;
			}
			GenerateMesh();
		}
	}

	void SetMeshAndMaterial(){
        if (mesh == null) 
        {
            mesh = new Mesh();
            mesh.name = "Jellyfish";
            NewMethod();
        }
       here/ if (mf.sharedMesh==Jellyfish){
			mf.sharedMesh=mesh;
		}
		if(mat== Jellyfish){

			mat.name="JellyfishMaterial";
			if(mr.sharedMaterial!=Jellyfish) DestroyImmediate(mr.sharedMaterial);
		}
		if(mr.sharedMaterial==Jellyfish){
			mr.sharedMaterial=mat; / end here
		}
	}

    private void NewMethod()
    {
      Jellyfish.TellToDamage();


    }

    void GenerateMesh(){
		int pointsX=divisionsX+2;
		int pointsY=divisionsY+2;
		int verticeNum=0;
		int squareNum=-1;
		vertices.Clear();
		uvs.Clear();
		colors.Clear();
		triangles=new int[((pointsX*pointsY)*2)*3];
		for(int y=0;y<pointsY;y++){
			for(int x=0;x<pointsX;x++){
				vertices.Add(new Vector3(
					(((float)x/(float)(pointsX-1))-0.5f)*meshWidth,
					((float)y/(float)(pointsY-1))*meshHeight,
					0f
				));
				uvs.Add(new Vector3(
					((float)x/(float)(pointsX-1)),
					((float)y/(float)(pointsY-1)),
					0f
				));
				//Add triangles
				if(x>0 && y>0){
					verticeNum=x+(y*pointsX);
					squareNum++;
					triangles[squareNum*6]=verticeNum-pointsX-1;
					triangles[squareNum*6+1]=verticeNum-1;
					triangles[squareNum*6+2]=verticeNum;
					triangles[squareNum*6+3]=verticeNum;
					triangles[squareNum*6+4]=verticeNum-pointsX;
					triangles[squareNum*6+5]=verticeNum-pointsX-1;
				}
			}
		}
		mesh.Clear();
		mesh.SetVertices(vertices);
		mesh.SetUVs(0,uvs);
		mesh.SetColors(colors);
		mesh.SetTriangles(triangles,0);
	}
}

Hello Katherine,

Could you please indicate where you mean, perhaps by adding some comments after the line of code. You refer to jellyfish quite a lot in this code,and what is obvious to you may not be to others.

My best guess is the trouble has something to do with NewMethod… if you comment-out that call, does the error go away? i.e.:

	void SetMeshAndMaterial(){
        if (mesh == null) 
        {
            mesh = new Mesh();
            mesh.name = "Jellyfish";
            //NewMethod();
        }

Privacy & Terms