ReImport in c# of GameObjects only for Scene Objects, not assets?

Hi, i want this to reimport specific GameObjects, in case they’re mess-up through manipulation, now I’ve got this issue now that it only find’s the real Path of the Asset, when they’re somewhere in the Scene (/Assets/Folder/Folder/GameObject.fbx) , else I’ve got the Error: Could not find a Part of the Path Exception…

So for a temporary Solution i’ve checked for scene GameObjects only, but that’s not what I really want achieve.


Basically I want to get the full Path from a GameObject, and then reload it via LoadAssetAtPath()


EDIT: Code “Debug Mode”
here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
[ExecuteInEditMode]
public class ReImport : MonoBehaviour {
	public bool Re_Import;
	public string realPath;
	public string selectedPath;
	public string correctFile;
	public GameObject Imported;
	// Use this for initialization
	
	// Update is called once per frame
	void Update () {
		if(Re_Import){
			reimp(Imported);
			Re_Import = false;
		}
	}
	void reimp(GameObject go){
			if(go.scene.IsValid()){
				realPath = Application.dataPath;
				realPath.Remove(realPath.Length -6);
				selectedPath = realPath + AssetDatabase.GetAssetPath(go);
				string[] fileEntries = Directory.GetFiles(selectedPath,"*",SearchOption.AllDirectories);
				for(int i = 0; i<fileEntries.Length; i++){
					string file = fileEntries*;*
  •  			file = file.Replace("\\","/");*
    
  •  			file = file.Remove(0,realPath.Length);*
    
  •  			if(file.Contains(PrefabUtility.GetCorrespondingObjectFromSource(go).name) && !file.Contains(".meta")){*
    
  •  				correctFile = file;*
    
  •  				// AssetDatabase.ImportAsset("Assets"+file);*
    
  •  				Debug.Log(" Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+correctFile+"</i></color>");*
    
  •  			}*
    
  •  		}*
    
  •  	}*
    
  •  	else{*
    
  •  		realPath = Application.dataPath;*
    
  •  		realPath.Remove(realPath.Length -6);*
    
  •  		selectedPath = realPath + AssetDatabase.GetAssetPath(go);*
    
  •  		correctFile = AssetDatabase.GetAssetPath(go);*
    
  •  		Debug.Log(" Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+correctFile+"</i></color>");*
    
  •  	}*
    

}
}

----------
Thanks for any Advice dan

thank’s to @xxmariofer for your wisdom! Finally I’ figured out what the Problem was. If you want to know the correct exact Path from a Scene Object you need to get the CorrespondingObjectFromSource first and then cast GetAssetAtPath.

here’s the working code for ReImporting either Scene Objects or Assets from a Folder!

		public static void ReImport(this GameObject go){
			string selectedPath;
			if(go.scene.IsValid()){
				Object realGO = PrefabUtility.GetCorrespondingObjectFromSource(go);
				selectedPath = AssetDatabase.GetAssetPath(realGO);
				AssetDatabase.ImportAsset(selectedPath);
				Debug.Log(" Scene Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+selectedPath+"</i></color>");
			}else{
				selectedPath = AssetDatabase.GetAssetPath(go);
				AssetDatabase.ImportAsset(selectedPath);
				Debug.Log(" Asset was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+selectedPath+"</i></color>");
			}
		}