Loses object while reloading scene

hello all,
I got stuck at very strange issue or bug. I have almost completed my game. When I played it multiple times, I found out that when I m reloading scene like retrying game then I loses a object. Each and every other object is okay. What I am doing is I stored some paths of sprites in a vector and at runtime, generating a random number in the range of vector size then picking up that path and creating a sprite with that path. Sometimes it works but sometimes it doesn’t. Each step is working (like I have paths in vector, random number is generating, it picks path from vector) but sprite is not creating (sprite is null pointer as result in this case). And this issue is same for back button, i.e. when I press back button and again press play, Sometimes it loads sprite but sometimes it doesn’t.
Can someone please help me in this.
Thanks.

What does the debugger say when you step over line by line?

I am using android studio

....
....
int random = rand()% 3;
log("random number = %d", random);
log("random number path = %s", staticWeapons.at(random).c_str());
weapon = Sprite::create(staticWeapons.at(random));
if(weapon == nullptr)
   log("weapon is null");
if(weapon != nullptr){
   log("weapon is not null");
...
...

and here is my log output:

It is neither responding to if( ==nullptr) nor to ( !=nullptr)

But what does your debugger say? It will show you an objects scope at the time and whatever the object contains, plus the call stack to see where it came from.

I did not use debugger for c++ yet in android studio because I have never encountered such issues. I will let you know after setting up debugger for c++ because normal android studio debugger is not responding to the break points of cocos code I found out some posts related to debugger or It would be great if you suggest me something.
I am sorry for the delay.

I tried using debugger but did not get something wrong. Meanwhile I noticed that it works for less code. Actually I was generating a sprite form either left or right side of the screen then further either normal or with rotation, so I used two variables( one for left/right side and another for normal/with rotation sprite) and declaring both of these variable locally. So just for try I defined them globally and rewrote that function with less redundant code and It worked… :grimacing:

Globally probably isn’t proper. Why not post some code and let the community help.

Actually earlier my function has very redundant code so I am writing it in short.
I have two vectors to store sprites paths: one for static weapons and another for rotating weapons

std::vector<std::string> staticWeapons, rotatingWeapons;  // in header file
staticWeapons.push_back("Weapons/StaticWeapon1.png");
staticWeapons.push_back("Weapons/StaticWeapon2.png");    
staticWeapons.push_back("Weapons/StaticWeapon3.png");    
rotatingWeapons.push_back("Weapons/RotatingWeapon1.png");    
rotatingWeapons.push_back("Weapons/RotatingWeapon2.png");    
rotatingWeapons.push_back("Weapons/RotatingWeapon3.png");

then in the function, I have two temporary variables: one for deciding either left or right side of the screen to position weapon and another to decide either static or rotating sprite and I was doing it with if else

int temp1 = rand() %2;
if(temp1 == 0) {
     // for left side
     int temp2 = rand() %2;
     if(temp2 == 0){
            // static weapon
     }
     else{
          // rotating weapon
      }
}
else{
    // for right side
    int temp2 = rand() %2;
    if(temp2 == 0){
            // static weapon
     }
     else{
          // rotating weapon
      }
}

and for creating weapon (in each section of if else with minor direction and rotation changes)

weapon = Sprite::create(staticWeapons.at(rand() %3));
if(weapon == nullptr)
     log("weapon is null");
else
     log("weapon is not null");
weapon->setPosition(......);
weapon->setScale(......);
weaponBody = PhysicsBody::createBox(weapon->getContentSize(),
                                            PhysicsMaterial(0, 1, 0)); 
weaponBody->setCollisionBitmask(2);
weaponBody->setContactTestBitmask(true);
weapon->setPhysicsBody(weaponBody);
this->addChild(weapon);
weaponTag = true;

weaponMovingSpeed = ......... //random speed in a range

auto moveToRight = MoveTo::create(vsWidth/weaponMovingSpeed, Point(vsWidth + weapon->getContentSize().width/2, weapon->getPosition().y));
weapon->runAction(moveToRight);
      
// addition rotation action

I know this is not a good way to code but I just wrote it like this because at first I want to make it workable then will reduce the code. :sweat_smile: :sweat_smile: