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 ![]()
I’m just gonna sit and wait patiently for a reply ![]()
Thanks in advantage,
Frisout