Hi! I don’t know how can I ask my question. My first script;
//MyFirstScript
...
public bool condition1;
public bool condition2;
public bool condition3;
public void CastRay()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
if (hit.transform != null)
{
condition1 = hit.transform.gameObject.tag == "A";
{
gameController.AddScore(scoreValue);
Destroy(hit.transform.gameObject);
}
condition2 = hit.transform.gameObject.tag == "B";
{
gameController.AddScore(scoreValue);
Destroy(hit.transform.gameObject);
}
condition3 = hit.transform.gameObject.tag == "C";
{
gameController.AddScore(scoreValue);
Destroy(hit.transform.gameObject);
}
}
else
{
Destroy(hit.transform.gameObject);
}
}
Now I wanna use these condition1,condition2 and condition3 like this at my other script;
//MyOtherScript
...
public void ShowOnScreen(GameObject[] thisList)
{
MyFirstScript s1 = gameObject.GetComponent<MyFirstScript>();
for (int i = 0; i < thisList.Length; i++)
{
Instantiate(thisList[i], Vector3.Lerp(startPoint, endPoint, (float)i / (thisList.Length - 1)), Quaternion.identity);
}
if (thisList == list1)
{
s1.condition1 || s1.condition2 || s1.condition3 // of course this line doesn't work :)
}
}
Is it possible? And how?
That makes no sense at all
condition1 = hit.transform.gameObject.tag == "A";
{
gameController.AddScore(scoreValue);
Destroy(hit.transform.gameObject);
}
you can do something like this:
condition1 = hit.transform.gameObject.tag == "A" ? true, false;
that is the short version of
if(hit.transform.gameObject.tag == "A")
{
condition1 = true;
}
else
{
condition1 = false;
}
Then you can do
if(condition1)
{
gameController.AddScore(scoreValue);
Destroy(hit.transform.gameObject);
}
if(s1.condition1|| s1.condition2|| s1.condition3)
{
//Do stuff?
}
Or what do you want to achieve?
Some basic C# tutorials wound not hurt you, if else is one of the most basics, and without that you will not get far.
1 Like
flonch
October 31, 2015, 3:28pm
3
What exactly is the issue? The way you do it in your second script seems correct, other than whatever line 13 is supposed to be.
I have prefabs like ball,pencil,car,apple. These are in the lists like this;
list1: car,apple
list2: ball,pencil,car
list3: pencil,car,ball,apple
I can show them on the screen with “ShowOnScreen” using “thisList*”. Than I wanna shoot what show on the screen. Example: ball’s tag is “A”,pencil’s tag is “B”,car’s tag is “C”.*
csharp* *if (thisList == list2) // when thisList == list2 I wanna shoot just ball(tag with "A"),pencil(tag with "B"),car(tag with "C") { s1.condition1|| s1.condition2|| s1.condition3 } // condition1 = hit.transform.gameObject.tag =="A" // condition2 = hit.transform.gameObject.tag =="B" // condition3 = hit.transform.gameObject.tag =="C"* *
flonch
October 31, 2015, 4:15pm
5
I think this is what you want to do.
if (thisList==list2 && (s1.condition1|| s1.condition2|| s1.condition3)) {
// Do something
}
Or perhaps this, if each condition has unique actions.
if (thisList==list2) {
if (s1.condition1) {
//
}
else if (s1.condition2) {
//
}
else if (s1.condition3) {
//
}
}
Sorry if I’m still not understanding.
Edit: Also, in MyOtherScript you should just do one GetComponent call at start and cache the result for efficiency.
@flonch Can I do this? I mean how can I ?
if (thisList == list2) //or thisList == list3 or thisList == list1
{
//do s1.condition1 || s1.condition2 [I]|| s1.condition3
}
flonch
November 1, 2015, 8:16pm
8
Sorry, I don’t really understand what you’re asking.
This line doesn’t make sense to me.
s1.condition1|| s1.condition2|| s1.condition3
Shouldn’t you be checking whether that line is true or false, then take action based on the result?
Reread fffMalzbier’s message.