Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Can't find Script after being added through code

Discussion in 'Editor & General Support' started by bh03ig, Jul 7, 2018.

  1. bh03ig

    bh03ig

    Joined:
    Jan 31, 2018
    Posts:
    13
    Hi,

    I have the following problem:

    Within the beginning script of my game (RTSManager.cs), I instantiate a "Unit" (Gameobject) prefab. The unit prefab already has a number of different Unity Components attached to it. After I declare the new unit, I add two new scripts as components through code. I also call an "Instantiate()" method on the newly created unit. This works fine.

    However, when it comes to finding the new units' components in the VisibilityManager.cs script, only the components which are present on the original prefab are found. Neither of the two components added through code are found or recognized, resulting in errors.

    My code is below. I modified this, so that the components on the gameobject are printed. This results in the UnitData component and RightClciknav component not being found (You can see these were the two added in the starting script.

    RTSManager.cs


    VisibilityManager.cs



    How can I resolve this issue?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    You're never adding
    NewUnit
    (the object you're adding the components to) to anything. Instead you're adding
    unit
    to the
    player.ActiveUnits
    list.
     
    bh03ig likes this.
  3. bh03ig

    bh03ig

    Joined:
    Jan 31, 2018
    Posts:
    13
    omg. Such an obvious mistake I am simply discarding the changes by not adding the modified Unit.

    Thanks a lot!