Cannot call class function

Okay, someone please help me before I bang my head against the wall again!

I have a class in one of my scripts.

I can create an instance of the class no problem.

I can address and even modify variables inside the class instance.

However I cannot access the functions built into the class. They simply do nothing.

code below:

GameManager.js

static function NewSelectionParticles(type : String, location : Transform)
{
	var newParts = new SelectionParticles(type,location);
	
	var script = location.gameObject.GetComponent(prefabCodeData);
	
	if (newParts)
	{
		switch (type)
		{
			case "iSel":
				script.myParticles = newParts;
				break;
				
			case "aSel":
				script.attackParticles = newParts;
				break;
			
			case "mSel":
				script.destParticles = newParts;
				break;
			
			case "sSel":
				script.targetParticles = newParts;
				break;
		}
	}
}

class SelectionParticles
{
	var blueColors : Color[] = GameManager.blueColors;
	var redColors : Color[] = GameManager.redColors;
	var yellowColors : Color[] = GameManager.yellowColors;
	var destColors : Color[] = GameManager.destColors;
	var currentParticles : GameObject;

	function SelectionParticles(type : String, location : Transform)
	{
		switch (type)
		{
			case "iSel":
				InitParticles(location,blueColors,type);
				break;
				
			case "aSel":
				InitParticles(location,redColors,type);
				break;
			
			case "mSel":
				InitParticles(location,destColors,type);
				break;
			
			case "sSel":
				InitParticles(location,yellowColors,type);
				break;
		}
	}
	
	function InitParticles(location : Transform, colorArray : Color[], type : String)
	{
		currentParticles = GameObject.Instantiate(GameManager.selectionParticles);
		currentParticles.transform.parent = location;
		var script = currentParticles.GetComponent(LockParticleTargetPosition);
		script.enabled = true;
		currentParticles.particleEmitter.emit = true;
		var getPartSys = currentParticles.GetComponent(ParticleAnimator);
		getPartSys.colorAnimation = colorArray;
	}

	function KillParticles()
	{
		print("Killing");
		currentParticles.particleEmitter.emit = false;
		while (currentParticles.particleEmitter.particleCount > 0)
		{
			yield WaitForSeconds(0.0);
		}
		GameObject.Destroy(currentParticles);
	}
}

I call the static function NewSelectionParticles(type,location) from another script.

NewSelection Particles sets up the class instance, and pushes it to a variable on the originating script.

From within that originating script I can access, for example, myParticles.currentParticles.particleEmitter.emit and set it to false. Or get the particle count, etc.

I cannot call myParticles.Killparticles();

This does nothing.

Help please! :slight_smile:

Jason

static functions are always called by Classname.FunctionName, not just FunctionName

myParticles is an instance of the class, so by calling myParticles.KillParticles() aren’t I using ClassName.FunctionName?

edit:

I tried myParticles.Selectionparticles.Killparticles() and that threw an error.

I have, however, narrowed it down to the yield statement in the killparticles function. If i remove the yield statement it works.

Help please :slight_smile:

If you’re going to ask for help on an error, you should at least report what the error message says. I’m going to guess it says something like you’re trying to use yield from a function that’s not a coroutine.

I’m not asking for help on an error. There is NO error, the function simply fails to execute. If you carefully read my initial post, it says ‘They simply do nothing’.

It has something to do with yield, remove yiled and the function is called. With yield in, nothing happens. Otherwise I would have stated there was an error.

Jason

Well, sorry, I’m confused - I saw you did say that the function did nothing earlier and then in the quote above you say it threw an error. Since you say removing yield makes it work (I assume by that you mean it doesn’t throw an error anymore), then I’m guessing (just guessing) you’re calling the whole thing in a context that that doesn’t allow calls to yield, in which case you should review the scripting doc on coroutines and yield.

Edit: Actually, what you might need is a yield from the calling code, like yield StartCoroutine(your-function-with-yield-in-it) to get the behavior you’re expecting. But I’ll stop guessing now.

The error was thrown when I tried dremora’s suggestion of using the class name to call the function. It didn’t work because I was already using the class name as an instanced variable. So i reverted back to my original call. In the meantime, I found that removing ‘yield’ from the class’ function KillParticles allowed the function to execute.

I did read what I could find on yield and coroutine and it didn’t help me any. I’m hoping some of you can be more helpful.

I apologize for my tone if you felt I was being unfriendly. Like I said, my last guess is that you need a yield in conjunction with the calling function - I ran into that confusion with coroutines before in my maze generation, where I had a yield in the generating code but it appeared to return instantly. Calling yield MazeGeneration() would wait for MazeGeneration to finish. However, if that is the problem, than I’d expect at least the lines of code before the inner call to yield to evidently complete. And you can’t use yield everywhere, e.g. not in Update (I don’t remember all the details, and I’m not going to RTFM right now - I am trying to provide free advice, after all)

Edit: well, I did RTFM a little - the page on MonoBehavior.StartCoroutine has examples of waiting and not-waiting for a function using yield to finish.

Thanks technicat. I’m not using yield in update, etc. It’s inside a javascript class and I’m simply using it to loop a While statement until an event and then destroy an object.

I took a look at my code and was able to do the while loop outside the class with a little fudging. I’d rather do it inside, but until I can find out how, I’ll do it this way.

Thanks for the insight :slight_smile:

Jason