problem switching back to first object (raycast loop?)

So I got a small problem.

What I want is a spell which temporarily switches focus to an object you can controll, and then with right click you switch back to the original player object. I am already very close with it! just this last detail doesnt seem to work: if I right click I can see that it switches the focus back to player for 1 frame, and then the focus goes back to the object you are temporarily controlling.

Here are the 2 scripts I am using:

Code attached to the player:

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

public class spellList : MonoBehaviour {

    //Some more random stuff above here, not needed
    public GameObject player;
    public Camera fpsCamera;
    public float range = 100f;

    void Update ()
    {
        spellName = mainInputField.text.ToLower() ;

        //Spells start here

//A lot of spells here which I removed to clear up space, they are not needed.

//Wingardium Leviosa
        else if (spellName.Contains ("wingardium leviosa")) //else since there were other scripts before
        {
            RaycastHit hit;
            if (Input.GetButtonDown ("mouse1"))
            {
                if (Physics.Raycast (fpsCamera.transform.position, fpsCamera.transform.forward, out hit, range))
                {
                    if (hit.transform.CompareTag ("leviosa"))
                    {
                        Debug.Log ("ey lullo");
                        wingardiumOn = true;
                    }

                }
            }
        }
        
//Everything off
        else
        {
            wingardiumOn = false;
        }
    }
}

Code attached to the temporarily controlable object:

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

public class wingardiumMovement : MonoBehaviour {

    public float Movespeed;
    public GameObject wingardiumCamera;
    public GameObject player;
    public spellList otherscript;

    void Update()
    {
        if (otherscript.GetComponent<spellList> ().wingardiumOn)
        {
               //The stuff I use to controll the object and set the player object to inactive
        }

        if (Input.GetButtonDown ("mouse2"))
        {
            wingardiumCamera.SetActive (false);
            player.SetActive (true);
            otherscript.GetComponent<spellList> ().mainInputField.text = ("");
        }
    }
}

What should happen to me is that, once the first script gets enabled again from the 2nd script, and the inputfield is made empty (which is working). so that means that the [else] at the end of the 1st script gets activated, which should Disable wingardiumOn, which should turn the stuff I use to controll the object off.

What in reality happens is that even the camera I turn off with ‘‘mouse2’’ gets turned on, so clearly as soon as script 1 gets activated, something in the script imidiatly activates everything in script 2 again, like something is looping. but I cant find what and am desperate :hushed:

I’m just gonna sit and wait patiently for a reply :smile:
Thanks in advantage,
Frisout

I may should have added that I did add multiple wingardiumOn = false; at other places to make sure it really does get deactivated. It’s just that it deactivates for a total of 1 frame and then hops back imidiatly

I have not found a fix yet :frowning:

I dont know if there are rules about bumping but this is my last call for help, this problem really bothers me and atleast some kind of answer would be nice

I cannot immediately see any particular problem, but I wanted to offer a small bit of advice that might help you :slight_smile:
Maybe try to really simply your script(s) - just have enable spell/camera and disable…
See if that works, then add stuff back into it and see how it goes…
Hope you get it solved :slight_smile:

1 Like

I think I can see the problem. Since you dont set wingardiumOn to false directly on the movement script the movements script update could occur before spell lists, so it would reactivate everything, then your spell list sets wingardiumOn to false, but since your move script doesnt disable itself in this case your now stucking casting while false.

There a few ways to fix this, in wigardium movement set wingardiumOn to false on mouse2.

Change wingardium moves update to a lateupdate so you know it happens last.

I would also recommend in WingardiumMovement have a bool for When its enabled & that way you can do:

Bool m_spellActive;

void Update
{
     if(otherScript.wingardiumOn)
     {
           if(!m_spellActive)
           {
                m_spellActive = true;
                //Initialise stuff here
           }
     }
     else if(m_spellActive)
     {
          m_spellActive = false;
          //UnIntialise here
     }
}

That way when wingardiumOn is false it will always be inactive.

1 Like

Thank you so much @lordconstant !
The void LateUpdate worked perfect! :smile: so glad I got this solved, and a lesson for the future :slight_smile:
Also thanks to @methos5k for trying to help

Glad ya got it resolved :slight_smile: