Do you know why there is an error?

Hello genius!
I’m a beginner and I need your help.
Why is there an error in the first line of this code?

int s_num = 40;

PShape s;
PShape b;

Virus Virus1 = new Virus[s_num];
Cell Cells= new Cell[40];

void setup()
{
size(800,600,P3D);

s = loadShape("Virus.obj");
b = loadShape("cell.obj");
s.scale(3.0);
 b.scale(20.0);

for(int i =0; i< s_num; i++){
Virus1[i] = new Virus();
}

for(int i=0; i< 1; i++){
Cells[i] = new Cell ();
}

}

void draw()
{
background(0);
for(int i =0; i < s_num; i++){
Virus1[i].display();
Virus1[i].move();
}
for(int i =0; i < 1; i++){
Cells[i].display();
Cells[i].move();
}

}
class Virus{

float xpos,ypos,zpos,speed,w;

Virus()
{
xpos = random(width);
ypos = random(height);
zpos = random(-200,-400);
speed = random(3,5);
//w = random(1.800);

  }

void display()
{
noStroke();
fill(#ffcc33);
pushMatrix();
translate(xpos,ypos,zpos);
shape(s,w,w);
popMatrix();
}

void move(){
zpos += speed;
if(zpos > 500 ){
zpos = -200;
}

}

}

class Cell{
float xpos,ypos,zpos,speed,w;

Cell()
{
xpos = mouseX;
ypos = mouseY;
zpos = mouseX;
speed = 5;
w = 0;
}

void display()
{
noStroke();
fill(255,5,188);
pushMatrix();
translate(xpos,ypos,zpos);
shape(b,w,w);
popMatrix();
}

void move()
{
xpos = mouseX;
ypos = mouseY;
}

}

Easy fix mate, you have to declare the virus and cell arrays with

Virus[] Virus1 = new Virus[s_num];
Cell[] Cells = new Cell[40];

Note the after the Virus and Cell type, you put it after the naming of them objects

1 Like

On second try, seems you can put it there, not sure then

1 Like

I substituted a cube and sphere in and it works on my pc, can you post the error code?, it could be the 3d obj file you’re trying to import

1 Like

this runs with text()

I also had to add the brackets [ ]




int s_num = 40;

PShape s;
PShape b;

Virus[] viruses = new Virus[s_num];
Cell[] cells= new Cell[40];

void setup() {
  size(800, 600, P3D);

  s = loadShape("Virus.obj");
  b = loadShape("cell.obj");
  //s.scale(3.0);
  //b.scale(20.0);

  for (int i =0; i< s_num; i++) {
    viruses[i] = new Virus();
  }

  for (int i=0; i< 40; i++) {
    cells[i] = new Cell ();
  }
}

void draw()
{
  background(0);
  for (int i =0; i < s_num; i++) {
    viruses[i].display();
    viruses[i].move();
  }
  for (int i =0; i < 40; i++) {
    cells[i].display();
    cells[i].move();
  }
}

// =====================================================================================

class Virus {

  float xpos, ypos, zpos, speed, w;

  Virus()
  {
    xpos = random(width);
    ypos = random(height);
    zpos = random(-200, -400);
    speed = random(3, 5);
    //w = random(1.800);
  }

  void display()
  {
    noStroke();
    fill(#ffcc33);
    pushMatrix();
    translate(xpos, ypos, zpos);
    shape(s, w, w);
    text("V", 0, 0); 
    popMatrix();
  }

  void move() {
    zpos += speed;
    if (zpos > 500 ) {
      zpos = -200;
    }
  }
}

// =====================================================================================

class Cell {
  float xpos, ypos, zpos, speed, w;

  Cell()
  {
    xpos = mouseX;
    ypos = mouseY;
    zpos = mouseX;
    speed = 5;
    w = 0;
  }

  void display()
  {
    noStroke();
    fill(255, 5, 188);
    pushMatrix();
    translate(xpos, ypos, zpos);
    shape(b, w, w);
    text("C", 0, 0); 
    popMatrix();
  }

  void move()
  {
    xpos = mouseX;
    ypos = mouseY;
  }
}
// =====================================================================================

1 Like

Thank you very much for your sincerity. Thanks to you, I solved it well!!!

2 Likes

Thank you so much! It worked out well!!

2 Likes