Problem with Pickup Script (Long)

Okay, so, in a way this is a continuation of my last post which involved swapping out Char A with Char B. Now, I’m using the Lerpz Tutorial script “Pickup” to have my character pick up different colored Gems.

Now, when the character starts out, he is able to pick up the gems and they properly increase the values on the Gem Counter Panel. However, when I switch forms, the Gems no longer increase the values on the counter panel, despite disappearing on Collision.

Furthermore, if I switch the forms BACK, the first form also cannot pick up the gems. What is going on here?

I suspect it has something to do with both Characters having the script “ThirdPersonStatus”, but the game is only able to recognize them one at a time. But I’m not sure. Any help is greatly appreciated!

Pickup:

enum PickupType { Health = 0, BlueGem = 1, YellowGem = 2, GreenGem = 3, RedGem = 4 }
var pickupType = PickupType.BlueGem;
var amount = 1;
var sound : AudioClip;
var soundVolume : float = 2.0;
private var used = false;
private var mover : DroppableMover;
function Start ()
{
	// do we exist in the level or are we instantiated by an enemy dying?
	mover = GetComponent(DroppableMover);
}

function ApplyPickup (playerStatus : ThirdPersonStatus)
{
	// A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial.
	switch (pickupType)
	{
		case PickupType.Health:
			playerStatus.AddHealth(amount);
			break;
		
		case PickupType.BlueGem:
			playerStatus.FoundBlue(amount);
			break;
			
		case PickupType.YellowGem:
			playerStatus.FoundYellow(amount);
			break;
		
		case PickupType.GreenGem:
			playerStatus.FoundGreen(amount);
			break;
		
		case PickupType.RedGem:
			playerStatus.FoundRed(amount);
			break;
	}
	
	return true;
}

function OnTriggerEnter (col : Collider) {
	if(mover  mover.enabled) return;
	var playerStatus : ThirdPersonStatus =  col.GetComponent(ThirdPersonStatus);
	
	//* Make sure we are running into a player
	//* prevent picking up the trigger twice, because destruction
	//  might be delayed until the animation has finished
	if (used || playerStatus == null)
		return;
	
	if (!ApplyPickup (playerStatus))
		return;

	used = true;
	
	// Play sound
	if (sound)
		AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
		
	
	
	// If there is an animation attached.
	// Play it.
	if (animation  animation.clip)
	{
		animation.Play();
		Destroy(gameObject, animation.clip.length);
	}
	else
	{
		Destroy(gameObject);
	}
}

// Auto setup the pickup
function Reset ()
{
	if (collider == null)	
		gameObject.AddComponent(BoxCollider);
	collider.isTrigger = true;
}

@script RequireComponent(SphereCollider)
@script AddComponentMenu("Third Person Props/Pickup")

ThirdPersonStatus

// ThirdPersonStatus: Handles the player's state machine.

// Keeps track of inventory, health, lives, etc.


var health : int = 6;
var maxHealth : int = 6;
var lives = 4;

// sound effects.
var struckSound: AudioClip;
var deathSound: AudioClip;

private var levelStateMachine : LevelStatus;		// link to script that handles the level-complete sequence.

private var BlueGemsOwned : int;	// Number of BlueGems the player has. Grabbed from LevelStatus.
private var YellowGemsOwned: int;
private var GreenGemsOwned: int;
private var RedGemsOwned: int;

function Awake()
{
	
	levelStateMachine = FindObjectOfType(LevelStatus);
	if (!levelStateMachine)
		Debug.Log("No link to Level Status");
	
	remainingBlue = levelStateMachine.BlueGemsOwned;
}

// Utility function used by HUD script:
function GetRemainingBlue() : int
{
	return BlueGemsOwned;
}

function GetRemainingYellow() : int
{
	return YellowGemsOwned;
}

function GetRemainingGreen() : int
{
	return GreenGemsOwned;
}

function GetRemainingRed() : int
{
	return RedGemsOwned;
}

function ApplyDamage (damage : int)
{
	if (struckSound)
		AudioSource.PlayClipAtPoint(struckSound, transform.position);	// play the 'player was struck' sound.

	health -= damage;
	if (health <= 0)
	{
		SendMessage("Die");
	}
}


function AddLife (powerUp : int)
{
	lives += powerUp;
	health = maxHealth;
}

function AddHealth (powerUp : int)
{
	health += powerUp;
	
	if (health>maxHealth)		// We can only show six segments in our HUD.
	{
		health=maxHealth;	
	}		
}


function FoundBlue (numFound: int)
{
	BlueGemsOwned += numFound;

// NOTE: We are deliberately not clamping this value to zero. 
// This allows for levels where the number of pickups is greater than the target number needed. 
// This also lets us speed up the testing process by temporarily reducing the collecatbles needed. 
// Our HUD will clamp to zero for us.

}

function FoundYellow (numFound: int)
{
	YellowGemsOwned += numFound;
}

function FoundGreen (numFound: int)
{
	GreenGemsOwned += numFound;
}

function FoundRed (numFound: int)
{
	RedGemsOwned += numFound;
}

function FalloutDeath ()
{
	Die();
	return;
}

function Die ()
{
	// play the death sound if available.
	if (deathSound)
	{
		AudioSource.PlayClipAtPoint(deathSound, transform.position);

	}
		
	lives--;
	health = maxHealth;
	
	if(lives < 0)
		Application.LoadLevel("GameOver");	
	
	// If we've reached here, the player still has lives remaining, so respawn.
	respawnPosition = Respawn.currentRespawn.transform.position;
	Camera.main.transform.position = respawnPosition - (transform.forward * 4) + Vector3.up;	// reset camera too
	// Hide the player briefly to give the death sound time to finish...
	SendMessage("HidePlayer");
	
	// Relocate the player. We need to do this or the camera will keep trying to focus on the (invisible) player where he's standing on top of the FalloutDeath box collider.
	transform.position = respawnPosition + Vector3.up;

	yield WaitForSeconds(1.6);	// give the sound time to complete. 
	
	// (NOTE: "HidePlayer" also disables the player controls.)

	SendMessage("ShowPlayer");	// Show the player again, ready for...	
	// ... the respawn point to play it's particle effect
	Respawn.currentRespawn.FireEffect ();
}

I think you’re right when you said the problem might be

. Each Character has its own set of variables, so you need instead to have a single object that has one set of variables. You could put this on the camera object maybe, or GUI. I’m not familiar with it, but maybe Static objects/classes can be used for this too?

Bump

Okay so I solved this problem. Very simple!

All I had to do was change the variables to static variables.

Now, one more problem. In my game, the character is able to freely roam around between different zones. Meaning, if he leaves zone 1 and goes to zone 2, he is free to return to zone 1 at any time.

The problem I’m having is that as soon as he returns to zone 1, all the gems he collected respawn. How can I make sure that the gems do not respawn?

One solution might be that gems register to collected items array. When you return to level, items in that array must deleted before game starts.

Other way around would be that everytime when game starts you have array where all gems are and when item is collected it’s removed from that array.

Sorry to be a pain, but could you be a little more specific please?

I’m a real amateur when it comes to scripting. How would I go about doing this?

No problem, I tried to be brief and not to bind your solution to idea which is inside my mind, just giving directions :smile:.

I’ll much prefer C#, so all codes will be also in C#.

Somewhere inside GameObject which stays alive whole time, or which stores every other gamedata you have ArrayList m_collectedGems; It’s allocated there also.

When gem is collected, lets make assumption that gems are always at same spots, so we can easily just decide that we use location specific solution :smile:

Inside that storing class you have method, which does following:

/**
* gemstoringMethod
* @param p_collectedGem, position of collected gem 
*/
public void gemstoringMethod( Vector3 p_collectedGem )
    {
    if ( m_collectedGems == null )
        {
        m_collectedGems= new ArrayList();
        }
    m_collectedGem.Add( p_collectedGem );
    }

Ok, that adds gem which was collected to place where you store your gamedata, so don’t forget to call this before destorying picked gem. Parameter is picked gems position.

Now when you get back from level2 to level1 first thing you do is loop through gems you got and remove those gems which are inside array. Snippet is inside somewhere which is done first when you arrive to level.

GameObject[] gems = GameObject.FindGameObjectsWithTag("gems"); // or how ever you're planning to find all gems from level :smile:

for ( int cIndex = 0; cIndex  <  m_collectedGems.Length; cIndex ++ )
    {
    for ( int mIndex = 0; mIndex  <  gems.Length; mIndex ++ )
        {
        if ( m_collected[cIndex] == gems[mIndex].transform.position )
            {
            // destroy this gem
            }
   
        }
    }

I did all coding directly here, so it won’t compile, it might not work, but at least you should get idea how it might work :smile:.