Switching between game objet on runtime

Hello community,

i’m working on a visualisation set and i have to be able to add and move some object in it.

actually i have attached to an empty gameobject some toggle script to make object appear.
i also have on each concerned game object a transform script to be able to move them in my set.
works nice but i need to be able to deactivate the transform script on some objet like

example i have a boat i place it in my set at a desirable spot and need to put my character on it.
i use this simple script to make them move

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

public class TransformFunctions : MonoBehaviour
{
public float moveSpeed = 10f;
public float turnSpeed = 50f;
public float climbSpeed = 5f;

void Update()
{
if (Input.GetKey(KeyCode.Keypad8))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

if (Input.GetKey(KeyCode.Keypad2))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);

if (Input.GetKey(KeyCode.Keypad7))
transform.Translate(-Vector3.right * moveSpeed * Time.deltaTime);

if (Input.GetKey(KeyCode.Keypad9))
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);

if (Input.GetKey(KeyCode.Keypad4))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);

if (Input.GetKey(KeyCode.Keypad6))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);

if (Input.GetKey(KeyCode.KeypadPlus)) {transform.position += transform.up * climbSpeed * Time.deltaTime;}
if (Input.GetKey(KeyCode.KeypadMinus)) {transform.position -= transform.up * climbSpeed * Time.deltaTime;}
}
}

my probleme is for now, i have to desactivate the boat to move my character if not, both will move so its too much complicated to work with.

considering that i must be able to move my camera to go anywhere i want.

so i need an idea of how to freeze a choosen object without hiding and being able to move another.

any idea ?

thanks by advance

well i may have i idea

is that possible to desactivate a script on runtime with a keyCode ?
like i wish to desactivate my transform function on my choosen game object with a shortcut ?

You can disable scripts from gameobjects:

boatTransform.getComponent<TransformFunctions>().enabled = false;

Thx you i m gonna try this !

well not as easy as i thought, my code level is very low.

i cant get to work this code below and i don’t konw why

```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AA_test : MonoBehaviour

{
public string gameObjects;

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

    if (Input.GetKey(KeyCode.Alpha3))
        gameObjects.GetComponent<TransformFunctions>().enabled = false;

}

}**
```

i’m getting this error i dont understand

Severity Code Description Project File Line Suppression State
Error CS1061 ‘GameObject’ does not contain a definition for ‘getComponent’ and no extension method ‘getComponent’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?) Noddy_S2_BoardTools_Generator D:\UNITY_Projects\Noddy_S2_BoardTools_Generator\Assets\AA_test.cs 13 Active

i’m also trying to make a toggle but it works one time
it diseable it and i cant re-enable it with this code, can’t figure why

```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptEnabler : MonoBehaviour {

[System.Serializable]
public class SwitchableArrays
{
    public GameObject[] gameObjects;

// public Behaviour behaviours;
}

public SwitchableArrays[] switchableGroups;
public KeyCode toggleKey = KeyCode.Alpha3;
public int currentlyActiveGroup;


void Update()
{
    if (Input.GetKeyDown(toggleKey))
    {
        Toggle();
    }
}

void Toggle()
{
    ChangeStates(currentlyActiveGroup, false);
    currentlyActiveGroup++;
    currentlyActiveGroup %= switchableGroups.Length;
    ChangeStates(currentlyActiveGroup, true);
}

void ChangeStates(int group, bool enable)
{
    foreach (GameObject objectToEnable in switchableGroups[group].gameObjects)
    {
        objectToEnable.GetComponent<TransformFunctions>().enabled = false;
    }

// foreach (Behaviour behaviourToEnable in switchableGroups[group].behaviours)
// {
// behaviourToEnable.GetComponent().enabled = true;
// }
}
}**
```

If any one got an Idea, it will be a good Christmas present :slight_smile:

thanks by advance

ok well found it ^^

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

public class ScriptEnabler : MonoBehaviour {

    [System.Serializable]
    public class SwitchableArrays
    {
        public GameObject[] gameObjects;
      }

    public SwitchableArrays[] switchableGroups;
    public KeyCode toggleKey = KeyCode.Alpha3;
    public int currentlyActiveGroup;
   
    void Update()
    {
        if (Input.GetKeyDown(toggleKey))
        {
            Toggle();
        }
    }

    void Toggle()
    {
        ChangeStates(currentlyActiveGroup, false);
        currentlyActiveGroup++;
        currentlyActiveGroup %= switchableGroups.Length;
        ChangeStates(currentlyActiveGroup, true);
    }

    void ChangeStates(int group, bool enable)
    {
        foreach (GameObject objectToEnable in switchableGroups[group].gameObjects)
        {
            objectToEnable.GetComponent<TransformFunctions>().enabled = enable;
        }

    }
}
1 Like