Lose collider GameObject script giving error

Hi. I changed the ball script and this worked fine. When I try on the LoseCollider script I get the error

Assets/Scripts/LoseCollider.cs(9,22): error CS1519: Unexpected symbol `=’ in class, struct, or interface member declaration

this is the script
gameobject

The error briefly stopped, but then came back on the next change from lecture chnaging ‘win’ to lose screen’

Any ideas??
Thanks

1 Like

Hi,

Pop this line;

levelManager = GameObject.FindObjectOfType<LevelManager>();

into a Start method;

void Start()
{
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}

Also, it’s a lot easier, with code, if you copy/paste it into the forum and then apply the code formatting characters before and after it. By doing so it allows those who help you to copy/paste your code back instead of having to type it out. Screenshots are however really useful when it comes to errors or details within the Unity editor.

Hope this helps :slight_smile:


See also;

The error has now gone, Thanks

It plays, But the win screen isn’t now coming up now when the ball leaves the screen

I get this in the console

NullReferenceException: Object reference not set to an instance of an object
LoseCollider.OnTriggerEnter2D (UnityEngine.Collider2D trigger) (at Assets/Scripts/LoseCollider.cs:20)

Hi Gareth,

Assuming line 20 is the line which is using the levelManager to load the “Win” scene, chances are this is because levelManager has returned null, e.g. in that Start method, it didn’t find an object of type LevelManager in the scene. Do you have a GameObject in your scene with the LevelManager script attached?

yes. Ive got an object called Level Manager and the script is on that

Can you copy/paste your code into a reply so I can see it please, and the LevelManager code too.

Here’s the Level manager script

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

public void LoadLevel(string name){
	Debug.Log ("Level load requested for: " +name);
	Application.LoadLevel(name);

}
public void QuitRequest(){
Debug.Log(“I want to quit!”);
Application.Quit();
}
}

And where it’s attached

level

…and the LoseCollider.cs?

using UnityEngine;
using System.Collections;

public class LoseCollider : MonoBehaviour {
	
	private LevelManager levelManager;
	
	void start()
	
	{
	
	levelManager = GameObject.FindObjectOfType<LevelManager>();
	
	}

	
	
		 void OnTriggerEnter2D (Collider2D trigger) {	
			levelManager.LoadLevel("Win");
			
		}
		
		void OnCollisionEnter2D (Collision2D collision) {
			print ("Collision");
		}

using UnityEngine;
using System.Collections;

public class LoseCollider : MonoBehaviour {
	
	private LevelManager levelManager;
	
	void start()
	
	{
	
	levelManager = GameObject.FindObjectOfType<LevelManager>();
	
	}

	
	
		 void OnTriggerEnter2D (Collider2D trigger) {	
			levelManager.LoadLevel("Win");
			
		}
		
		void OnCollisionEnter2D (Collision2D collision) {
			print ("Collision");
		}

Hi,

Thanks for posting the code, please remember to use the code formatting though, it makes it easier for us to read.

So the issue is that your Start method has been spelt with a lower-case “s”. C# is case-sensitive, so in this scenario, Unity hasn’t run the code within your start method because it doesn’t consider it to be one of it’s Start methods.

If you correct that you should find that your levelManager gets initialised with the GameObject.FindObjectOfType statement and then things will work.

I noticed that you appear to be missing a closing curly brace from the code above also, perhaps that was just a copy/paste error, worth checking too though.

Hope this helps. :slight_smile:

All working, Great stuff. Thank you:grin:

Sorry but how do you use the code formatting? I’m just copying directly from the script
This is all new to me!

1 Like

Hi Gareth,

Good to hear and you are very welcome.

I placed a link in one of my earlier replies for you with regards to the code formatting, we have a Forum User Guide for it :slight_smile:

Great, sorry I missed it. I’ll sort it for next time.
My air con has failed today. At 28 degrees solving coding errors was proving challenging to say the least. :rage: Thanks again

1 Like

No worries, they are just there for support but it does help those that offer to help etc.

There are a few others also that may be worth looking at, for embedding video content and images etc as and when required.

Sorry to hear about the a/c, mine exists only in the form of an open window but we are having a hot one today too… finally got myself inside in the shade for a bit! :slight_smile:

Privacy & Terms