Z
Z
zeni1agent2022-03-06 22:47:58
C++ / C#
zeni1agent, 2022-03-06 22:47:58

How to add information from another script to the list?

Sorry if this is a stupid question, but I couldn't find an answer on the internet.
But how to run new MyInfo("name1") Inside another

File1Scr script

public class MyInfo
  {
    string Name;
    
    public Card(string name)
    {
      Name = name;
    }
    
  }



  // Работает
  list.Add(new MyInfo("name1"));


File2Scr
File1Scr MyInfo;


  // Не работает
  list.Add(new MyInfo("name1"));


Unity texts me

'MyInfo' does not contain a constructor that takes 1 arguments

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nekit Medvedev, 2022-03-09
@NIKROTOS

You need a link to an object.
In a unit, an analogue of an object is a component, and you need to get it, for example, drag it into the corresponding field of the final object. To define access to a field, access modifiers are used, you need public. In the programming society, it is customary not to use public fields, but to give access to their VALUES through methods, for example:

public string getName()
{
return Name;
}

Or change them via methods:
public void setName( string Value)
{
Name=Value;
}

To be more precise, you simply create a public field, the class of the component (script) you need and move the object with this script into this field (with the mouse). This is the easiest option, if you need to automate this process, see the documentation on how to find the object, at the getComponent food object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question