architecture – Instantiating GameObjects in Custom Game Engine

So,i’m having a bit of an issue with instantiating my gameobjects into to universe(My Scene object).

I can create an empty object from scratch and populate it from there with ease,my problem starts when i try to clone the gameobject which i just created.

My steps are

  1. I create an empty gameobject (GAMEOBJECT A)
  2. I add an component to it (COMPONENT A)
  3. Now i create a gameobject B from A
  4. GameObject B created,and the component A is in the component list of the gameobject B
  5. I try to get the component using inheritance but nullptr returns

It seems when i try to clone components,it returns only the base class pointer.There i try to recreate the derived class and add it to new gameobject’s component list but obviously i fail.

  • If i cant solve this with inheritance how can i solve it ?
  • is there better way to implement such a desing ?
  • or i just fail to use inheritance in c++.If it’s, can you guys point out what am i doing wrong here

This is my GameObject AKA RGameElement

enter image description here

This is my Component AKA RElementComponent

enter image description here

and this my Instantiate via gameobject ref function

enter image description here

In Unity, I’m trying to understand what code gets generated when I attach scripts to GameObjects

I’m trying to observe any file changes that happen when I attach a script to a GameObject.

I am able to attach a script to a GameObject, and verify that it was attached by looking at the console log. (In my case I attached MainPlayer to some random GameObject like WeaponCamera.)

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

public class MainPlayer : MonoBehaviour
{
    public string myName;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("I am alive and my name is " + myName);
    }

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

However, I don’t see any actual code change (eg: line additions) to any files in my unity project (using git status or git diff). I would think that something would get updated in my repo, unless I’m misunderstanding how attachment works? For example, if someone else were to copy my repo and try it on their computer, shouldn’t they have the attachment I just made?

unity – Panning value of 2nd GameObject’s AudioSource is not initializing

I m making a prototype and stuck in a problem where 2 gameobjects swap but there values of panStereo of AudioSource is not initializing from a float array which contains all the Default Value of panStereo of all the gameObject in a scene

 panningArray = new float(Inventory.bowlsManager.BowlPanningValues.Length);
    for (int i = 0; i < Inventory.bowlsManager.BowlPanningValues.Length; i++)
    {
        panningArray(i)=Inventory.bowlsManager.BowlPanningValues(i);
    }
    Selectable = false;
    SelectedBowl.GetComponent<AudioSource>().spatialBlend = SelectedBowl2.GetComponent<AudioSource>().spatialBlend = 1;
    iTween.MoveTo(SelectedBowl, iTween.Hash("position", temp2, "time", transitionspeed));
    iTween.MoveTo(SelectedBowl2, iTween.Hash("position", temp, "time", transitionspeed));

    SelectedBowl.GetComponent<Renderer>().material = SubsituteMaterial;
    SelectedBowl2.GetComponent<Renderer>().material = SubsituteMaterial;
    int SelectedBowlIndex = Array.FindIndex(Inventory.allBowls.ToArray(), x => x == SelectedBowl.GetComponent<Bowl>()); ;
    
    int SelectedBowlIndex2 = Array.FindIndex(Inventory.allBowls.ToArray(), x => x == SelectedBowl2.GetComponent<Bowl>());
    print(Inventory.bowlsManager.BowlPanningValues(SelectedBowlIndex2));
    print(Inventory.bowlsManager.BowlPanningValues(SelectedBowlIndex));

   val1=  BowlPanning(SelectedBowl2 , panningArray(SelectedBowlIndex));
     val2= BowlPanning(SelectedBowl , panningArray(SelectedBowlIndex2) );

Here is My Bowl panning Function:

 public float BowlPanning(GameObject BowlRequire,float value)
{
    BowlRequire.GetComponent<AudioSource>().panStereo = value;

    return BowlRequire.GetComponent<AudioSource>().panStereo;

}

unity – Instantiate random different gameObjects in specific spaces?

I need to instantiate different gameObjects of different shapes and sizes on top of the left third and the right third of a table, and all this objects need to be random every time the scene loads. I have a library of around 80 GO but the most I’ll instantiate, for now, is 30. All the instantiated GO have to be different, that means no duplicates.

I thought that maybe I could create 30 empty gameObjects on the table, where I want to instantiate them in random, but I’m not sure if this is the most intelligent way to do it. Is there any other way?

Also, since I have a library of GO so large, would it be possible to load the folder in the script instead of defining a list and populating it in the editor?

I’m a bit new at coding for Unity, but I’m an experienced programmer.

How can I get the basic Unity player controller to move based on a gameobjects rotation?

I am making a basic movement script based off of the built in playercontroller in Unity. I am trying to get the Player gameobject to move forward based on its own rotation (i.e. if it is rotated to 90 degrees, it will go forward in the direction it is facing and not based off of its transform values), but I can’t figure out how to change it.

How can I do this?

How can I debug a GameObject’s rotation?

I have a model of a dragon, with an animation of it flying. When I play the animation in the Animator’s preview, the dragon flies in a straight line, but when I run it in-game, it flies around in circles about 10 units in diameter.

If the turning isn’t coming from the animator, it has to be coming from somewhere else, but I have no idea where. There aren’t any scripts on the dragon that would influence it like that. Is there any way to discover what’s causing my dragon to turn? (It’s times like this that I really miss the ability to set a memory breakpoint on an object when working in .NET. That would make this trivial!)

c # – Unity, check if all tagged GameObjects are destroyed

Hi, I'm a total newbie, so don't blame me.
So I have this code but can't find a proper answer on how to check if everything tagged as "Target1" is destroyed. Most of the answers are too complicated or I'm certainly too stupid

public Transform teleportTarget;
public GameObject thePlayer;
public Transform() spawnLocations;
public GameObject() whatToSpawnPrefab;
public GameObject() whatToSpawnClone;

void spawnTargets()
{
    whatToSpawnClone(0) = Instantiate(whatToSpawnPrefab(0), spawnLocations(0).transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
    whatToSpawnClone(1) = Instantiate(whatToSpawnPrefab(1), spawnLocations(1).transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
    whatToSpawnClone(2) = Instantiate(whatToSpawnPrefab(2), spawnLocations(2).transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
    whatToSpawnClone(3) = Instantiate(whatToSpawnPrefab(3), spawnLocations(3).transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
}

private void TeleportPlayer()
{
    thePlayer.transform.position = teleportTarget.transform.position;
}

I'm just looking for a way to check if all GameObjects tagged as "Target1" (in the entire game)
they are destroyed and then generate SparTargets and TeleportPlayer.
Previously I had the error, that when they reappeared I was no longer checking. You have to check all the time, because otherwise I get stuck, because teleporting teleports me all the time. I hope you understand what I mean

I know this is an easy question, but I'm a newbie and I'm sorry I wasted my time but I hope you can help me.

unit – How to select most GameObjects in a stack of gameObjects?

Instead of putting a script with a OnMouseDown method on each object of the game, create one game object with an update function that

  1. applications Input.GetMouseButtonDown(0), Input.GetAxis("Mouse X") Y Input.GetAxis("Mouse Y") read mouse status
  2. convert the position of the mouse on the screen in lightning from the camera position to the scene using Camera.ScreenPointToRay.
  3. Detect the first collider that touches that ray using Physics.Raycast or Physics2D.Raycast depending on whether your objects have 3d or 2d colliders.

This, of course, requires that the order of representation of your SpriteRenderers be managed by their z-position, not by representation layers or orders in those representation layers.

Code example (not tested!)

void Update() {

      if (Input.GetMouseButtonDown(0)) {
            Vector3 mousePosition = new Vector3(Input.GetAxis("Mouse X"),
                                                Input.GetAxis("Mouse Y"),
                                                0.0f);
            Ray mouseRay = Camera.main.ScreenPointToRay(mousePosition); 
            RaycastHit hit;
            if(Physics.Raycast(mouseRay, out hit)) {
                  GameObject clickedObject = hit.collider.gameObject;
                  // do what you want to do with clickedObject
            };
      }
}

An advantage of this architecture over OnMouseDown is that it decouples the input logic of the game mechanics. If you later want to add different ways of selecting objects, you do not need to implement that logic in the scripts that handle the game mechanics of selectable objects. You can contain these selection methods in separate scripts. The object that was clicked may know that it is selected, but you do not need to know exactly how it was selected.

unity – How to find ObjectGameObjectsWithTag and then convert the set of GameObjects into an array of interfaces

I have some GameObjects that implement a common interface: IsTree.
I need to call the doTreeThing () function on each of those IsTrees after obtaining a reference to each of them through FindGameObjectsWithTag ("tree").

This line complains that it cannot implicitly convert from GameObject () to IsTree ()

IsTree() trees = GameObject.FindGameObjectsWithTag("tree");

Then I tried to let the GameObject () iterate over it to convert each individual object into an IsTree.

GameObject() trees = GameObject.FindGameObjectsWithTag("tree");
        foreach (GameObject tree in trees) {
            IsTree isTree = (IsTree)tree;
            isTree.doTreeThing();
        }

But this complains that it cannot convert GameObject to IsTree either.

Then I got a little desperate and tried to turn my IsTree into a GameObject.

public class IsTree : GameObject {
    public System.DateTime getAge(){
        return null;
    }
}

GameObject is a sealed type. (I really don't know what that means besides this doesn't work)

So how do I get this GameObjects array in a SomeInterface array?

c # – Conversion of Gameobjects units to ECS

I haven't worked with ECS before, so I was following Mike Geig's Unity tutorial on YouTube to convert GameObjects to ECS from Unite Copenhagen, and this code does nothing, and there are no errors:

using Unity.Entities;

using Unity.Transforms;
using UnityEngine;

public class SpawnAsteroidECS: MonoBehavior
{
GameObject AsfaroidPrefab public;

EntityManager manager;
Entity AsteroidEntityPrefab;

void Start()
{
    manager = World.Active.EntityManager;
    AsteroidEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(AsteroidPrefab, World.Active);
}

void Update()
{
    if (Input.GetKey(KeyCode.Mouse0))
    {
        for (int i = 0; i < 100; i++)
            SpawnTheAsteroids();
    }
}

private void SpawnTheAsteroids()
{
    Entity asteroid = manager.Instantiate(AsteroidEntityPrefab);

    manager.SetComponentData(asteroid, new Translation { Value = transform.position });
    manager.SetComponentData(asteroid, new Rotation { Value = Quaternion.identity });
    print("Done");
}

}

What am i not doing

EDIT: The entity debugger shows that there are entities, but I don't really see it in the game.