My script doesnt work

Hi fellow Programmers, I cant get my c# script to work. its suposed to lift up a few Objects that I have put around my game-world. how can I make it work?


using System.Collections.Generic;
using UnityEngine;

public class PropControl : MonoBehaviour
{
//Variablen
public GameObject Object1;
public GameObject Object2;
public GameObject Object3;
public GameObject O1;
public GameObject O2;
public GameObject O3;

// Start is called before the first frame update
void Start()
{
O1.SetActive (true);
O2.SetActive (true);
O3.SetActive (true);
Object1.SetActive (false);
Object2.SetActive (false);
Object3.SetActive (false);
}

// Update is called once per frame
void OnTriggerStay(Collider other)
{
Debug.Log (“LOG”);
if (other.name == “Weapon1”)
Object1.SetActive (true);
Object2.SetActive (false);
Object3.SetActive (false);
if (other.name == “Weapon2”)
Object2.SetActive (true);
Object1.SetActive (false);
Object3.SetActive (false);
if (other.name == “Knife”)
Object3.SetActive (true);
Object1.SetActive (false);
Object2.SetActive (false);
}
}[/code]

Use the code tag properly so your code isn’t text but formatted code…

You know that only the first line after the if is in the conditional, because you didn’t define a proper { } block…

3 Likes

here ya go

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PropControl : MonoBehaviour
{
    //Variablen
   public GameObject Object1;
   public GameObject Object2;
   public GameObject Object3;


    // Start is called before the first frame update
    void Start()
    {
     Object1.SetActive (false);
     Object2.SetActive (false);
     Object3.SetActive (false);
    }

    // Update is called once per frame
    void OnTriggerStay(Collider other)
    {
     Debug.Log ("LOG");
  if (other.name == "Weapon1")
     Object1.SetActive (true);
     Object2.SetActive (false);
     Object3.SetActive (false);
  if (other.name == "Weapon2")
     Object2.SetActive (true);
     Object1.SetActive (false);
     Object3.SetActive (false);
  if (other.name == "Knife")
     Object3.SetActive (true);
     Object1.SetActive (false);
     Object2.SetActive (false);
    }
}

so the problem is that i need to swap the objects. but that doesnt work.

Re-read Lurk’s comment carefully:

Your if() clauses don’t have blocks to enclose their actions. Fix that first.

NOTE: C# is NOT Python. Whitespace means nothing as far as program control goes.

After that, if it still doesn’t work, it is time for you to learn how to debug!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

1 Like

You need to wrap up the code that is being executed with the if statements with { and } symbols. I also don’t recommend re-ordering the methods within them because at a glance it’s not immediately obvious why you did it and that can lead to code that is more difficult to debug.

if (other.name == "Weapon1")
{
    Object1.SetActive (true);
    Object2.SetActive (false);
    Object3.SetActive (false);
}
if (other.name == "Weapon2")
{
    Object2.SetActive (true);
    Object1.SetActive (false);
    Object3.SetActive (false);
}
if (other.name == "Knife")
{
    Object3.SetActive (true);
    Object1.SetActive (false);
    Object2.SetActive (false);
}

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements

1 Like

Even though this overall approach is not great, your 3 if-statemens could be removed completely since you set the active state of all your objects in each case. You can simply do

Object1.SetActive (other.name == "Weapon1");
Object2.SetActive (other.name == "Weapon2");
Object3.SetActive (other.name == "Knife");

When this code runs and the name iw “Weapon1”, only the first object will be activated and all the others would be deactivated because the boolean expression in the other cases is false.

Though your code does not really pick something up but switches the active weapon depending on what object you collide with. So i’m not really sure if what your script actually does what it should do. If you just wanted to switch the weapon when you collide with the corresponding object in your world, then yes, that’s what those 3 lines would do.

2 Likes

Thanks guys :wink: i just realised that. i put a break to rogramming games for like two weeks, and forgot most of the “Grammatik”:sweat_smile:

1 Like