The first script is very long, and I need to use only the top of it:

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

public class mBitmap2MeshV30a : MonoBehaviour
    public Texture2D SourceTexture; 
    private float Pixel_vs_Unit_Scale = 1;  
    public float extrudeDepth = 1; 
    private Vector2 scanpoint;
    private bool gotStart = false;
    private int maxsteps = 512; 
    public bool invertFaces = false;
    private Mesh srcMesh;
    private mMeshExtrusion2.Edge[] precomputedEdges;
    private List<ExtrudedTrailSectionBM3> sections;

    public class ExtrudedTrailSectionBM3
        public Vector3 point;
        public Matrix4x4 matrix;
        public float time;

I need to assign a value to SourceTexture from another script. This is the other script:

void Start ()
    text2d = new Texture2D(10, 10, TextureFormat.ARGB32, false);
    System.Drawing.Font myf = new System.Drawing.Font(FontFamily.GenericSerif, 12);
    Image myimage =  DrawText("Hello", myf, System.Drawing.Color.Red, System.Drawing.Color.Black);
    Bitmap mybitmap = new Bitmap(myimage);

    MemoryStream msFinger = new MemoryStream();
    mybitmap.Save(msFinger, mybitmap.RawFormat);

    filter.GetComponent<Renderer>().material.mainTexture = text2d;

    mBitmap2MeshV30a bb = new mBitmap2MeshV30a();
    bb.SourceTexture = text2d;

The problem is I can't make a new mBitmap2MeshV30a bb = new mBitmap2MeshV30a();. Both scripts are attached to the same GameObject, in the hierarchy.

  • \$\begingroup\$ You don't construct MonoBehaviours with new. Are you looking for GetComponent to access an existing component, or AddComponent to attach a new component? \$\endgroup\$ – DMGregory Sep 30 '17 at 0:36

instead of doing this mBitmap2MeshV30a bb = new mBitmap2MeshV30a(); make that class singleton.

to create any class as singleton you nees to make an static instance of that class by public static mBitmap2MeshV30a instance after creating instance you can provide the instance value to object by providing instance=this in void Awake() after providing instance you can call any function by mBitmap2MeshV30a.instance.SourceTexture.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.