Easy quest.. problem (variables)

Hi everybody, :smile:
I have a really simple problem… and i don’t get it in c#…

I have a woman who gives a quest. “Find 5 mushrooms!”

I have two scripts:
intro.cs
mushroom.cs

The woman has the intro.cs. That works perfekt.

Now I have 5 mushrooms in the level and I attached this script:

using UnityEngine;
using System.Collections;

public class mushroom : MonoBehaviour {
	
	public int counter=0;

	void Start () {
		
	
	}
	
	// Update is called once per frame
	void Update () {

	}
	
	void OnTriggerEnter()
	{
		counter++;
		Destroy(gameObject);	
	}
	
	void OnTriggerExit(){
		
	}
	
	
	void OnGUI(){
	  //   GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
		GUI.Box (new Rect(Screen.width/4,10, 20,20), counter + ""); 
	}
}

The mushrooms disappear, when I’m next to them. That works fine, but the counter for the mushrooms does not change…
But the GUI windows disappear when I collect all mushrooms…
How can I display the number of mushrooms. (i think that happens because it it isn’t in the update function, because if
i increase the counter in the update… this works)

Can you also give me an example, how i can access the variable?
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I don’t really get it.

Thank you! Hope you can help me :stuck_out_tongue:

Is this script attached to each mushroom? It looks so. What you need is a manager of sorts. This will handle the counter. So, ontriggerenter, managerData.boostMushroomCount(); Destroy(gameObject);

///mushroom.cs
var manager : GameObject;
var ManagerData : managerScript;

function Start()
{
managerData = manager.GetComponent(managerScript);
}

function OnTriggerEnter(other : Colloder)
{
if(other.tag == "Player")
{
managerData.boostMushroomCount();
Destroy(gameObject);
}
}

Then, in the manager script, have a function boostMushroomCount() that increases the counter value. Use the manager to display the GUI score.

Ok thank for your reply. (Yes you’re right, I attached my old script to each mushroom.)

Unfortunately I did not fully understand everything. :rage:
So i will have two scripts.
mushroom.cs
managerScipt.cs

mushroom.cs = = manager of sorts (where should I attach this script?
managerScript.cs → boostMushroomCount() and OnGUI

You would attach the mushroom script to each mushroom. This script, handles the trigger function and it knows who its manager is, so that it can access the managers script. This is done thru getComponent().

The manager script handles the mushroom count and the display of the mushroom count. It can be attached to an empty gameObject.

Thank you very very much :smile:
Finally I understand this :wink:

Ok now I have the next Problem :wink: I want to learn C# and this is Javascript:

    ///mushroom.cs
    var manager : GameObject;
    var ManagerData : managerScript;
     
    function Start()
    {
    managerData = manager.GetComponent(managerScript);
    }
     
    function OnTriggerEnter(other : Colloder)
    {
    if(other.tag == "Player")
    {
    managerData.boostMushroomCount();
    Destroy(gameObject);
    }
    }

I tried to convert this to C#, but I think I made mistakes in line 10:
(Cannot implicitly convert type “managerScropt” to “UnityEngine.Transform”

using UnityEngine;
using System.Collections;

public class mushroom : MonoBehaviour {
	public GameObject manager;
	public Transform curTransform;

	// Use this for initialization
	void Start () {
	curTransform = manager.GetComponent("managerScript") as ManagerScript;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

####################################
And another small question. How can I
access this variable in another Script. (for example the Variable counter)
I don’t get it :frowning:

Thank you very much!

I do not use C# but it looks like you have defined a var as a Transform that should be a script. Can you post your other script?

My intro.cs is attached to a woman.
She gives the quest.
I want to check in the quest1.cs, whether
5 mushrooms have been found. (counter == 5) ?? → then for example +500 gold

And this is my managerScript in c#

U]managerScript.cs[/U]

using UnityEngine;
using System.Collections;

public class managerScript : MonoBehaviour {

int counter = 0;


void boostMushroomCount()
	{
		counter++;
		
	}
	
void OnGUI(){
        GUI.Box (new Rect(Screen.width/4,10, 80,80), counter + "");

    }
}

quest1:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]

public class quest1 : MonoBehaviour {
	
	
 	int counter = 0;
	bool showIntro = false;
	bool quest1=false;

	
	void OnGUI(){

		
		if(showIntro !quest1){
		Time.timeScale=0;
		GUILayout.BeginArea(new Rect(10,10,200,200));
		GUILayout.Label ("Please find 5 mushrooms for me!");
		GUILayout.Label ("They are outside of the village!");
		GUILayout.Label ("I'm scared");
		
			
			if(GUILayout.Button ("No Problem.")){
				quest1=true;	
				Time.timeScale=1;
				showIntro = false;
				
			}
			if(GUILayout.Button ("Are you kiding me? Why th hell...")){
				
				Time.timeScale=1;
				showIntro = false;
			}
		GUILayout.EndArea ();
		}
		
		if(showIntro  quest1){
		GUILayout.Label ("Thanks!");
		GUILayout.Label ("Now I can make my soup...");
		Time.timeScale=1;
		showIntro = false;			
		}
	
		
		
	}
	
	void OnTriggerEnter(){
		showIntro = true;
		audio.Play();
	}
	
	void OnTriggerExit(){
		showIntro = false;
	}	
}

The issue is curTransform. It is classed as a Transform. Transforms only hold position, scale and rotation vectors. You need to class a var as the name of the script you want to access.

var manangerData : managerScript;

Now I have converted this Javascript to C#. How can I access this variable counter
in another script?
I tried it with 3 students… but we don’t get it…
For example in a file called intro.cs I want to access to the variable counter

ManagerScript.cs

using UnityEngine;
using System.Collections;

public class managerScript : MonoBehaviour {

int counter = 0;


public void boostMushroomCount()
	{
		counter++;
		
	}	
	
 void OnGUI(){
        GUI.Box (new Rect(Screen.width/4,10, 80,80), counter + "");

    }	
}

mushroom.cs

using UnityEngine;
using System.Collections;

public class mushroom : MonoBehaviour {

public GameObject manager;

public managerScript ManagerData;

 

void  Start (){

ManagerData = manager.GetComponent<managerScript>();

}
	
	
    void  OnTriggerEnter ( Collider other  ){
  //  if(other.tag == "Player")
  //  {
    ManagerData.boostMushroomCount();
    Destroy(gameObject);
  //  }
}
}

Just a short answer for your question getting a script from another object.

Referencing non static variables from another script? C# - Questions & Answers - Unity Discussions :slight_smile:

Thank you very much :smile: I also found this answer, but sometimes
it helps to look twice, thrice or for four times. Finally my first Quest works fine :sunglasses:

Glad to help and see, that it works now :slight_smile: Wish you a good workflow for the next quests :slight_smile:

Unfortunately it is not over yet :rage:
I have a litte problem (my script is very strange for me) :smile:

When I start my game in the Unity Editor - everything works perfekt… The hole Quest works!!!
Now I made a short publish test… and the Script did not work any more…
I thought about it for many hours … and when I was totally angry about this, I changed a small
thing… better… two lines… Now I am able to grab my mushrooms… but the counter doesn’t work any more…(but I think better than nothing) :wink:
I think, that the problem is in the following lines:

 void  OnTriggerEnter ( Collider other  )
	{	    
   	 if(other.tag == "Player")
   	 {
		Destroy(gameObject);
	    ManagerData.boostMushroomCount();
     }
	}
}

ALL SCRIPTS:

mushroom.cs:

using UnityEngine;
using System.Collections;

public class mushroom : MonoBehaviour {

public GameObject manager;

public managerScript ManagerData;

 

void  Start (){
ManagerData = manager.GetComponent<managerScript>();

}	
    void  OnTriggerEnter ( Collider other  )
	{	    
   	 if(other.tag == "Player")
   	 {
		Destroy(gameObject);
	    ManagerData.boostMushroomCount();
     }
	}
}

managerScropt.cs:

using UnityEngine;
using System.Collections;

public class managerScript : MonoBehaviour {

public int counter = 0;


public void boostMushroomCount()
	{
		counter++;
		
	}	
}

In my quest1 Script I wrote this in the update function:

	  void Update() {
	 GameObject thePlayer = GameObject.Find("GameObject");
	managerScript playerScript = thePlayer.GetComponent<managerScript>();
	zahl=playerScript.counter;
    }

And I call this with “GUI.Box (new Rect(5,Screen.width/6, 200,400), “Quest-Book” + “\n\n” + “Mushrooms:” + zahl +”/5");"

This is not the best way… I’m pretty sure :wink:

Hope you see why this first did not work (grabbing of the mushrooms + counter)
And when I changed two lines… so first remove the object… the object is deleted…
(In the Editor both works :wink: )

Thank you!

Where should I call the function: [ManagerData.boostMushroomCount()?
If I call this in the onTriggerEnter, nothing happens :frowning:

void OnTriggerEnter ( Collider other )
{
// if(other.tag == “Player”)
// {
Destroy(gameObject);
ManagerData.boostMushroomCount();
// }
}

You should switch the two lines, so it calls the mushroom count and then destroys the gameObject. Otherwise you say, destroy the object and then continue the script ( from a destroyed object ) :slight_smile:

Hi, I also thought that was my mistake but when I switch the two lines again, nothing will happen. (I can walk through the
mushrooms (collider not detected?) and the counter doesn’t work.) A friend also tried to publish the project on his computer.

Destroy(gameObject);
ManagerData.boostMushroomCount();
→ Object is destroyed, counter does not work

ManagerData.boostMushroomCount();
Destroy(gameObject);
→ Object is not destroyed and counter also does not work

Could you try to delete the Destroy function and watch your counter. Does it work? You can send me your project if you want so I could take a look.

Ok thank you :wink: I try it in the evening one more time. If it does not work, I send you my project or a small testproject. Now I have
to do some work for university :smile: