Put me out of my misery

Soldato
Joined
10 Mar 2006
Posts
3,975
One question I promise, before I go and chuck myself into the fire.

What about this code is wrong? I'm trying to instantiate a particle effect in Unity3D using a simple piece of C#. Trying to learn 3D modelling whilst teaching myself programming again.

Code:
using UnityEngine;
using System.Collections;

public class CoinPickup : MonoBehaviour {

	// Update is called once per frame
	void OnTriggerEnter (Collider  collision) {
		if (collision.tag == "Player") {
			GameObject anEffect = Instantiate(CoinEffect, transform.position, transform.rotation) as GameObject;
			Destroy(anEffect);
		}
	}
}

I get the error

Assets/CoinPickup.cs(12,62): error CS0103: The name `CoinEffect' does not exist in the current context

Is 'Instantiate' even a C# operator/thingy? Any help appreciated. If I don't get it working I promise I'll up some photos of my burning carcass.
 
Soldato
OP
Joined
10 Mar 2006
Posts
3,975
Thanks AMMUT, I think I understand it now. You're right - I feed it the gameObject and it then uses the transform.position/rotation to locate where it should be instantiated. The particle effect pops out of a coin I have setup.

I've got it working with this:

Code:
	public void OnTriggerEnter (Collider  collision) {

		Debug.Log ("OnTriggerEnter activated");
		if (collision.tag == "Player") {
			Debug.Log("Player tag recognised");
			GameObject anEffect = Instantiate(CoinPickupEffect, transform.position, transform.rotation) as GameObject;
			Destroy(anEffect, 1);
			Destroy(gameObject);
		}
	}

Now I'm up against needing to create a new instance of a class that I've just created. I need to be able to use it to store a score value, so the player increases their score as they collect coins (of which there is currently only one, but hey).

GameMaster class (that I need creating, I think)
Code:
using UnityEngine;
using System.Collections;

public class GameMaster : MonoBehaviour {


	public int currentScore = 0;
	private int internalScore  = 0; 

	public void Update () {
		internalScore = currentScore;
	}

	public void ShowScoreDebug() {
		Debug.Log (currentScore.ToString ());
	}

	// Use this for initialization
	void GUI () {
	
	}
}

Then it's called from here, but I get the error after the code:

Code:
using UnityEngine;
using System.Collections;

public class CoinPickup : MonoBehaviour {

	public GameObject CoinPickupEffect;

	// Update is called once per frame
	public void OnTriggerEnter (Collider  collision) {

		Debug.Log ("OnTriggerEnter activated");
		if (collision.tag == "Player") {
			GameMaster.currentScore += 1;
			Debug.Log("Player tag recognised");
			GameObject anEffect = Instantiate(CoinPickupEffect, transform.position, transform.rotation) as GameObject;
			Destroy(anEffect, 1);
			Destroy(gameObject);
			GameMaster.Update();
		}
	}
}

Error:

Assets/Scripts/CoinPickup.cs(13,36): error CS0120: An object reference is required to access non-static member `GameMaster.currentScore'

I have a feeling if I can actually properly work with classes, and creating objects, that I might have the freedom and flexibility to start making/learning new code, so any help would be massively appreciated.

EDIT:

Looks like I should be using a Static Class? I'll rework the code in the next post as I'm still getting errors.
 
Last edited:
Soldato
OP
Joined
10 Mar 2006
Posts
3,975
Code:
using UnityEngine;
using System.Collections;

class GameMaster : MonoBehaviour {


	static public int currentScore = 0;
	static private int internalScore  = 0; 

	static public void Update () {
		internalScore = currentScore;
		Debug.Log (internalScore);
	}

	static public void ShowScoreDebug() {
		Debug.Log (currentScore.ToString ());
	}

//	// Use this for initialization
//	void GUI () {
//	
//	}
}

This appears to work - a case of first not understanding where to use Static classes, then a case of not being able to implement them.
 
Soldato
OP
Joined
10 Mar 2006
Posts
3,975
I'm not sure to be honest - what would you recommend? It's meant to just be a way of handling the scores for the player, or players.

If there were more than one player, would you recommend it being a regular public class, where I create instances of it per player?

I can't seem to create a new instance of a class, that's where I fall down.
 
Associate
Joined
10 Nov 2013
Posts
1,808
I don't have much experience of c# so apologies for any duff syntax, but if it were me I'd have a regular class and create an instance of for each player. Then in future as your game gets more complicated you can just extend it to manage any new states, etc.

To create an object of your class you can just do:

Code:
GameMaster gameObj = new GameMaster();

You can add a constructor to your class which will get called when you 'new' a class

Code:
public GameMaster()
{
    internalScore = 0;
    // some other init stuff...
}
 
Back
Top Bottom