"Talking" Script, all worked and broke after " Build"..

Hello,

its my first post on this forum, so I want to greeting with all of us.

I’m creating “Game” on my ingeener project for school, and I have a small problem.

I create: collecting cash and stars system and all work well.

I start create “Talking” Script too and when I done, all work well.

Problem’s show, when i using button " build run ", and:

At start console show me new errors, but on this forum i find resolve.

" if u have this error, in hierarhy do CTRL + A , DELETE, and CTRL + V" All ok i done it, all scipts r in self possition. I Start second time… and ? And northing ! Unity3d ignore my script, don’t do northing, and consolwe dont write any errors.

I start new project, write all from new… and i have same problem , but before compile all work well ;S

It’s my code script’s for it:

intera.cs :

using UnityEngine;
using System.Collections;

public class intera : MonoBehaviour {
	
	private GameObject ply ;
	private GameObject cam;
		
	public GUIText target;
	private bool selected =false;
	private bool menu =false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		renderer.material.color=Color.white;
		selected = false; 
	}
	
	
		public void OnLookEnter(){
			renderer.material.color=Color.red;
			target.text ="Nacisnij E by porozmawiac";
			selected = true;
	}
	
	void OnGUI(){
		ply = GameObject.Find("First Person Controller");
		cam = GameObject.Find("Main Camera");
		Event e = Event.current;
		if(e.isKey  e.character == 'e'  selected ){
			//rigidbody.AddForce(Vector3.up*100);
			ply.GetComponent<MouseLook>().enabled = false;
			ply.GetComponent<CharacterMotor>().enabled = false;
			ply.GetComponent<FPSInputController>().enabled = false;
			cam.GetComponent<MouseLook>().enabled = false;
			Screen.showCursor = true;
			menu = true;
		}
		if(menu){
			GUI.Box(new Rect(0, (Screen.height/3)*2,Screen.width, Screen.height/3), " Dialog Test");
		
		target.enabled = false;
			
			
			if(GUI.Button(new Rect((Screen.width/2)-25,(Screen.height/4)*3, 50, 30), "Talk")){
			ply.GetComponent<MouseLook>().enabled = true;
			ply.GetComponent<CharacterMotor>().enabled = true;
			ply.GetComponent<FPSInputController>().enabled = true;
			cam.GetComponent<MouseLook>().enabled = true;
			Screen.showCursor = false;
			menu = false;
			target.enabled = true;
		
				
				
				
				}
		}
		
		
		
	}
}

selec.cs

using UnityEngine;
using System.Collections;

public class selec : MonoBehaviour {
	public RaycastHit hit;
	// Use this for initialization
	void Awake () {
		
		Screen.showCursor = false;
		
	}
	
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
		if(Physics.Raycast(ray, out hit, 10)){
		if(hit.collider.gameObject.GetComponent<intera>() !=null ){		
		hit.collider.gameObject.GetComponent<intera>().OnLookEnter();
			}	
		}
		
	}
}

poup.cs

using UnityEngine;
using System.Collections;

public class poup : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	guiText.text ="";
		
	}
}

Any1 could help me ?

OFC after resolve my problem, if any1 want to use this script, should feel free.

What I do bad?

Why when i do " play button " all works well but when i compile and run, all destroy.

P.S.

Stars, and cash script work well all time, after Build and before Build.

thx

Michal

Careful about using Find and Get Components inside OnGUI

e.g. ply = GameObject.Find(“First Person Controller”);

These only need to be done once, generally in Start. Remember, OnGUI is called twice per frame.

First try to isolate these instances in a separate function and only have OnGUI and Update changing/performing actions that need to be constantly updated.

PW

Thx for fast answer,

If I good undestand you,
I swap GameFinder’s for Start()

	void Start () {
	ply = GameObject.Find("First Person Controller");
		cam = GameObject.Find("Main Camera");
	}

but it doesnt help.

Northing happen, script donesnt start, but still in console i dont have any error’s, and all compile well.

I try all OnGUI script swap for void Start() { and remove OnGUI ()

and at effect i get this error:

NullReferenceException: Object reference not set an instance of an object.
problem is

intera Start() cs 19

then its this:

        ply = GameObject.Find("First Person Controller");

        cam = GameObject.Find("Main Camera");

        Event e = Event.current;

THIS LINE>>>>        if(e.isKey  e.character == 'e'  selected ){

            //rigidbody.AddForce(Vector3.up*100);

            ply.GetComponent<MouseLook>().enabled = false;

            ply.GetComponent<CharacterMotor>().enabled = false;

            ply.GetComponent<FPSInputController>().enabled = false;

            cam.GetComponent<MouseLook>().enabled = false;

            Screen.showCursor = true;

            menu = true;

Ok, next thing to check is your Event script.

If you are not careful, it will stay active when you don’t expect it. Last button pressed remains last button pressed until another button is pressed.

I tend to do things like:

keyPressed = Event.current;
if (keyPressed.current.type == EventType.KeyDown   keyPressed.keyCode == KeyCode.Space ) { keyPressedStatus = true; }

Then set keyPressedStatus to false once you have acted on it.

I am not sure where you have reset your select boolean.

PW

The event script should be in the OnGUI

Now,

i Don’t know what u mean,

If u want to swap my event “e”.

We should have this:

using UnityEngine;
using System.Collections;

public class intera : MonoBehaviour {
	
	private GameObject ply ;
	private GameObject cam;
		
	public GUIText target;
	private bool selected =false;
	private bool menu =false;
	// Use this for initialization
void Start () {
	ply = GameObject.Find("First Person Controller");
		cam = GameObject.Find("Main Camera");
			//rigidbody.AddForce(Vector3.up*100);
			ply.GetComponent<MouseLook>().enabled = false;
			ply.GetComponent<CharacterMotor>().enabled = false;
			ply.GetComponent<FPSInputController>().enabled = false;
			cam.GetComponent<MouseLook>().enabled = false;
			Screen.showCursor = true;
			menu = true;
		
		if(menu){
			GUI.Box(new Rect(0, (Screen.height/3)*2,Screen.width, Screen.height/3), " Dialog Test");
		
		target.enabled = false;
			
			
			if(GUI.Button(new Rect((Screen.width/2)-25,(Screen.height/4)*3, 50, 30), "Talk")){
			ply.GetComponent<MouseLook>().enabled = true;
			ply.GetComponent<CharacterMotor>().enabled = true;
			ply.GetComponent<FPSInputController>().enabled = true;
			cam.GetComponent<MouseLook>().enabled = true;
			Screen.showCursor = false;
			menu = false;
			target.enabled = true;
		
				
			}
	}
	}
	// Update is called once per frame
	void Update () {
		renderer.material.color=Color.white;
		selected = false; 
	}
	
	
		public void OnLookEnter(){
			renderer.material.color=Color.red;
			target.text ="Nacisnij E by porozmawiac";
			selected = true;
	}
	
	void OnGUI(){
	
		keyPressed = Event.current;
if (keyPressed.current.type == EventType.KeyDown   keyPressed.keyCode == KeyCode.Space ) { keyPressedStatus = true; }
				keyPressedStatus=false;
				
	
		}
		
		
		
	
}

But console respond :

Problem with Event start when I relocate OnGUI Script for Start()

before console don’t back this error.

IMeybe I wrong undestand u, and I do it bad.

if u Could, check it.

Don’t for get to define your vars

private var keyPressed : Event;
private var keyPressedStatus : boolean = false;

Also, an interaction should then set the boolean to false in your OnGUI, eg

	keyPressed = Event.current;
	if (keyPressed.current.type == EventType.KeyDown   keyPressed.keyCode == KeyCode.Space ) { keyPressedStatus = true; }


		if (GUI.Button (pressOpenButton,"",openCloseStyle) || keyPressedStatus ) {	
		  keyPressedStatus = false; OpenCloseTab(1); } }

I don’t know how do it.

But in my opinion var’s r using in js, not in C+.

could u add it in my code?

In all time I have error’s

Sorry meybe im stupid, or something else, but i do not have idea.

You going to have to battle through this one a bit more, 01:00 for me, I gotto go

Your C# vars are:

	private Event keyPressed;
    	private bool keyPressedStatus = false;

All your GUI code eg:

        if(menu){
            GUI.Box(new Rect(0, (Screen.height/3)*2,Screen.width, Screen.height/3), " Dialog Test");

            if(GUI.Button(new Rect((Screen.width/2)-25,(Screen.height/4)*3, 50, 30), "Talk")){
//etc

HAS to be inside the OnGUI function. YOU will need to re-organise things better. Your Start function will only initialise vars. The OnGUI is continuously updating its contents.

See how you go - if I get time I will have a look later this morning.

PW

I do some other, i do new project, copy all sript’s what i had, but i DELETE ALL asset’s without controller, and now all work fine, but if u have better resolve for event and my problem i ll happy for help, cause always I want do " it " better if its possible.

I ll edit this post and i ll say u what change.