- Home /
function does not act on gameobject when called from another script
Someone could tell me why when I call the "playing1" function from a button (using the inspector's OnClick and selecting the object to which the script is attached) it performs the "print (" enter ");" and the "indica1" .SetActive (true); ", but when calling this function from another script, just execute" print ("enter"); "?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Principal : MonoBehaviour
{
public GameObject indica1;
(...)
public void playing1(string _velocity)
{
print("enter");
indica1.SetActive(true);
}
}
follows the excerpts of the script where the call is made:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System.IO.Ports;
using System;
using System.Threading;
public class UnitySerialPort : MonoBehaviour
{
Principal principal;
void Start ()
{
principal = gameObject.GetComponent<Principal>();
string valorComando = "100";
}
(...)
private void GenericSerialLoop ()
{
switch (comando){
case "p":
principal.playing1(valorComando);
break;
}
}
}
Answer by tormentoarmagedoom · Apr 08, 2020 at 07:56 AM
Hello.
You tellig you have this function:
public void playing1(string _velocity)
attached to a button Click event? Did you debugged the code? How is the value of _velocity assigned?
Thank's for your time. The button calls this function and sends "50". But the point is that both methods are able to call the function and execute the print. The problem is that by calling the function from another script, the function does not act on objects (for example: gameobjectX.setActive (true);).
Your answer
