1

I made 2 scripts and assigned it to 2 cubes: "cube" and "cube 1". Normally if you click on cube it sets a value so that when you click cube 1 it disappears. If you click cube 1 first it's not gonna work. So that's what I tried to make but it does not work and I don't understand why.

here are my scripts

cube:

using UnityEngine; using System.Collections; public class script : MonoBehaviour { public int test = 0; // make the variable for the value public void OnMouseDown() // when the user click { test = 1; //make the value of test 1 } } 

cube 1:

using UnityEngine; using System.Collections; public class NewBehaviourScript1 : MonoBehaviour { public GameObject a; //make a gameobject public script script; //make a variable where we put in the script void OnMouseDown() // when the user click { script = a.GetComponent<script>(); // get script if ( script.test == 1) //test or the variable test in the other script is 1 { Destroy(gameObject); // destroy the object } } } 

Can someone please help me?

1
  • Can you add what actually is happening right now? EDIT: I'm sortof new to unity, but maybe, if you check the docs for GetComponent(), it returns the component attached to the object you are calling the function on. So... probably do something like cube.GetCompoment()? Idk, just my guess. Commented May 23, 2016 at 17:43

2 Answers 2

1

Change the name of the script class to be capitalised.

public class Script : MonoBehaviour 

Then in the NewBehaviourScript1 change everything inside it to:

public class NewBehaviourScript1 : MonoBehaviour { public Script script; //Drag the other cube onto this in the inspector. void OnMouseDown() { if ( script.test == 1) { Destroy(gameObject); } } } 

You should use more descriptive names for both your classes and their instances.

Note: For this to work you will have to have drag the other cube onto the script variable in the inspector and it will have to have a Script attached to it.

Sign up to request clarification or add additional context in comments.

Comments

0

Try using a.GetComponent<script>();. You need to pass a Type to GetComponent in order for it to work. I'm not sure that your code even compiles, its very hard to read it because you did not format it properly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.